将矩形UIImageView变成一个圆形

问题描述:

我的ViewController有一个UIImageView(让它命名为img)作为属性,并且在开始时,它将全屏显示img,即它的大小是一个类似于ViewController边界的矩形。将矩形UIImageView变成一个圆形

我想用img做一个动画,使它变成一个小圆圈(不只是一个圆角的矩形),然后将它移动到ViewController的某个角落。

我该如何做到这一点?我宁愿不发送任何代码因为我很确定我做了错误的方式(我试图把它变成一个正方形,然后设置一个角半径(半径宽度为2),然后制作比例和平移位置,但它的一个烂摊子,错误地工作,所以让我们忘记它)

+2

采取的ImageView与高度和宽度相同,并设置它的图层属性:imageView.layer.masksToBounds =真 imageView.layer.cornerRadius = img.frame.size.height/2 –

属性您的解决方案听起来不错,给我,但可能会出错的是UIView.animate不支持动画角点半径的事实WithDuration如View Programming Guide for iOS所示。因此,结果有时不是预期的结果。

你可以试试下面的代码。

func animate() { 

    //Some properties that needs to be set on UIImageView 
    self.imageView.clipsToBounds = true 
    self.imageView.contentMode = .ScaleAspectFill 

    //Create animations 
    let cornerRadiusAnim = changeCornerRadiusAnimation() 
    let squareAnim = makeSquareAnimation() 
    let boundsAnim = changeBoundsAnimation() 
    let positionAnim = changePositionAnimation(CGPointMake(300,480)) 

    //Use group for sequenced execution of animations 
    let animationGroup = CAAnimationGroup() 
    animationGroup.animations = [cornerRadiusAnim, squareAnim, boundsAnim, positionAnim] 
    animationGroup.fillMode = kCAFillModeForwards 
    animationGroup.removedOnCompletion = false 
    animationGroup.duration = positionAnim.beginTime + positionAnim.duration 
    imageView.layer.addAnimation(animationGroup, forKey: nil) 

} 

func makeSquareAnimation() -> CABasicAnimation { 

    let center = imageView.center 
    let animation = CABasicAnimation(keyPath:"bounds") 
    animation.duration = 0.1; 
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 
    animation.fromValue = NSValue(CGRect: imageView.frame) 
    animation.toValue = NSValue(CGRect: CGRectMake(center.x,center.y,imageView.frame.width,imageView.frame.width)) 
    animation.fillMode = kCAFillModeForwards 
    animation.removedOnCompletion = false 
    return animation 

} 

func changeBoundsAnimation() -> CABasicAnimation { 

    let animation = CABasicAnimation(keyPath:"transform.scale") 
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 
    animation.fromValue = imageView.layer.mask?.valueForKeyPath("transform.scale") 
    animation.toValue = 0.1 
    animation.duration = 0.5 
    animation.beginTime = 0.1 
    animation.fillMode = kCAFillModeForwards 
    animation.removedOnCompletion = false 
    return animation 

} 

func changeCornerRadiusAnimation() -> CABasicAnimation { 

    let animation = CABasicAnimation(keyPath:"cornerRadius") 
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 
    animation.fromValue = 0 
    animation.toValue = imageView.frame.size.width * 0.5 
    animation.duration = 0.1 
    animation.fillMode = kCAFillModeForwards 
    animation.removedOnCompletion = false 
    return animation 

} 

func changePositionAnimation(newPosition: CGPoint) -> CABasicAnimation { 

    let animation = CABasicAnimation(keyPath: "position") 
    animation.fromValue = NSValue(CGPoint: imageView.layer.position) 
    animation.toValue = NSValue(CGPoint: newPosition) 
    animation.duration = 0.3 
    animation.beginTime = 0.6 
    animation.fillMode = kCAFillModeForwards 
    animation.removedOnCompletion = false 
    return animation 
} 
+0

OMG,我不知道如果我应该说在这里,但我爱你 – Daniel

+0

@Daniel快乐,我可以帮助 –

//试试这个,我希望它能为你工作。

UIView.animateWithDuration(3, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {() -> Void in 

     img.layer.masksToBounds = true 
     img.layer.cornerRadius = img.frame.size.height/2 // here img must be in square shape (height == width) 

     }) { (finished : Bool) -> Void in 

} 

选项1调用以下方法从代码

// UIimageView Circle 
class func roundImageView(imageView:UIImageView){ 
    imageView.layer.borderWidth = 1.0 
    imageView.layer.masksToBounds = false 
    //imageView.layer.borderColor = UIColor.whiteColor().CGColor 
    imageView.layer.cornerRadius = imageView.frame.size.width/2 
    imageView.clipsToBounds = true 
} 

选项2化妆用户定义的运行时间从情节串连图板

view.layer.cornerRadius = 5.0f; 
view.layer.masksToBounds = NO;