UIView contentstretch 将圆形更改为椭圆形,而不是药丸形状

Posted

技术标签:

【中文标题】UIView contentstretch 将圆形更改为椭圆形,而不是药丸形状【英文标题】:UIView contentstretch changes circle to oval, not to pill shape 【发布时间】:2011-06-03 18:29:34 【问题描述】:

我有一个 80 x 80 单位的圆形图像,并试图使用 UIImageView 的 contentstretch 属性将其更改为药丸形状,但我只能得到一个椭圆形。有任何想法吗?这是我的代码:

imageView.frame = CGRectMake(0,0,80,80);
imageView.layer.contents = (id)([UIImage imageNamed:@"circle.png"]).CGImage;
imageView.contentStretch = CGRectMake(0.5, 0,0, 1);
[UIView animateWithDuration:0.5 delay: 0.0 options: UIViewAnimationOptionTransitionNone
animations:^
    imageView.frame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y, imageView.frame.size.width+50, imageView.frame.size.height);
        
        completion:^(BOOL finished)

                             ];

【问题讨论】:

奇怪。当我假设你想要一个矩形在中间时,你的代码可以正常工作。您是否更改了任何其他图像视图设置。我直接从 IB 中使用它。 我刚刚再次尝试使用全新的图像视图。我没有设置其他任何东西。它只是变成了一个椭圆形。 您可以查看this 感谢您的解决。可能是我的图像尺寸不完全是 80 像素导致问题 【参考方案1】:

你试过了吗?

UIImage *circleImage = [UIImage imageNamed:@"circle.png"];
UIImage *stretchImage = [circleImage stretchableImageWithLeftCapWidth:40 topCapHeight:40];
imageView.image = stretchImage;

【讨论】:

以上是关于UIView contentstretch 将圆形更改为椭圆形,而不是药丸形状的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UIView 裁剪为半圆形?

UIView 圆形遮罩动画

给予uiview animation有哪几种动画曲线

多个 UIView 的动画与 UIBezierPath

UILabel 在圆形自定义 UIView 中不可见

如何使用 Swift 在 UIView 中将圆形进度条居中?