缩放UIButtonanimationSwift

我试图为UIButton做点击时的缩放animation,但是我试图完成的是点击button时,我需要UIButton缩小到里面,然后它回到它的大小(如泡泡)。

我尝试了以下内容:

 button.transform = CGAffineTransformMakeScale(-1, 1) UIView.animateWithDuration(0.5, animations: { () -> Void in button.transform = CGAffineTransformMakeScale(1,1) }) 

尝试这个

 UIView.animate(withDuration: 0.6, animations: { self.button.transform = CGAffineTransform(scaleX: 0.6, y: 0.6) }, completion: { _ in UIView.animate(withDuration: 0.6) { self.button.transform = CGAffineTransform.identity } }) 

SWIFT3代码更新 :我有一个很好的弹跳效果的animationbutton,弹簧animation。

 @IBOutlet weak var button: UIButton! @IBAction func animateButton(sender: UIButton) { sender.transform = CGAffineTransform(scaleX: 0.6, y: 0.6) UIView.animate(withDuration: 2.0, delay: 0, usingSpringWithDamping: CGFloat(0.20), initialSpringVelocity: CGFloat(6.0), options: UIViewAnimationOptions.allowUserInteraction, animations: { sender.transform = CGAffineTransform.identity }, completion: { Void in() } ) } 

以上所有答案都是有效的。
另外,使用Swift,我build议创build一个UIView的扩展,以便“缩放”你想要的任何视图。
你可以从这段代码中获得灵感:

 extension UIView { /** Simply zooming in of a view: set view scale to 0 and zoom to Identity on 'duration' time interval. - parameter duration: animation duration */ func zoomIn(duration duration: NSTimeInterval = 0.2) { self.transform = CGAffineTransformMakeScale(0.0, 0.0) UIView.animateWithDuration(duration, delay: 0.0, options: [.CurveLinear], animations: { () -> Void in self.transform = CGAffineTransformIdentity }) { (animationCompleted: Bool) -> Void in } } /** Simply zooming out of a view: set view scale to Identity and zoom out to 0 on 'duration' time interval. - parameter duration: animation duration */ func zoomOut(duration duration: NSTimeInterval = 0.2) { self.transform = CGAffineTransformIdentity UIView.animateWithDuration(duration, delay: 0.0, options: [.CurveLinear], animations: { () -> Void in self.transform = CGAffineTransformMakeScale(0.0, 0.0) }) { (animationCompleted: Bool) -> Void in } } /** Zoom in any view with specified offset magnification. - parameter duration: animation duration. - parameter easingOffset: easing offset. */ func zoomInWithEasing(duration duration: NSTimeInterval = 0.2, easingOffset: CGFloat = 0.2) { let easeScale = 1.0 + easingOffset let easingDuration = NSTimeInterval(easingOffset) * duration / NSTimeInterval(easeScale) let scalingDuration = duration - easingDuration UIView.animateWithDuration(scalingDuration, delay: 0.0, options: .CurveEaseIn, animations: { () -> Void in self.transform = CGAffineTransformMakeScale(easeScale, easeScale) }, completion: { (completed: Bool) -> Void in UIView.animateWithDuration(easingDuration, delay: 0.0, options: .CurveEaseOut, animations: { () -> Void in self.transform = CGAffineTransformIdentity }, completion: { (completed: Bool) -> Void in }) }) } /** Zoom out any view with specified offset magnification. - parameter duration: animation duration. - parameter easingOffset: easing offset. */ func zoomOutWithEasing(duration duration: NSTimeInterval = 0.2, easingOffset: CGFloat = 0.2) { let easeScale = 1.0 + easingOffset let easingDuration = NSTimeInterval(easingOffset) * duration / NSTimeInterval(easeScale) let scalingDuration = duration - easingDuration UIView.animateWithDuration(easingDuration, delay: 0.0, options: .CurveEaseOut, animations: { () -> Void in self.transform = CGAffineTransformMakeScale(easeScale, easeScale) }, completion: { (completed: Bool) -> Void in UIView.animateWithDuration(scalingDuration, delay: 0.0, options: .CurveEaseOut, animations: { () -> Void in self.transform = CGAffineTransformMakeScale(0.0, 0.0) }, completion: { (completed: Bool) -> Void in }) }) } } 

用法很简单:

 let button = UIButton(frame: frame) button.zoomIn() // here the magic 

Swift 3版本

 extension UIView { /** Simply zooming in of a view: set view scale to 0 and zoom to Identity on 'duration' time interval. - parameter duration: animation duration */ func zoomIn(duration: TimeInterval = 0.2) { self.transform = CGAffineTransform(scaleX: 0.0, y: 0.0) UIView.animate(withDuration: duration, delay: 0.0, options: [.curveLinear], animations: { () -> Void in self.transform = CGAffineTransform.identity }) { (animationCompleted: Bool) -> Void in } } /** Simply zooming out of a view: set view scale to Identity and zoom out to 0 on 'duration' time interval. - parameter duration: animation duration */ func zoomOut(duration: TimeInterval = 0.2) { self.transform = CGAffineTransform.identity UIView.animate(withDuration: duration, delay: 0.0, options: [.curveLinear], animations: { () -> Void in self.transform = CGAffineTransform(scaleX: 0.0, y: 0.0) }) { (animationCompleted: Bool) -> Void in } } /** Zoom in any view with specified offset magnification. - parameter duration: animation duration. - parameter easingOffset: easing offset. */ func zoomInWithEasing(duration: TimeInterval = 0.2, easingOffset: CGFloat = 0.2) { let easeScale = 1.0 + easingOffset let easingDuration = TimeInterval(easingOffset) * duration / TimeInterval(easeScale) let scalingDuration = duration - easingDuration UIView.animate(withDuration: scalingDuration, delay: 0.0, options: .curveEaseIn, animations: { () -> Void in self.transform = CGAffineTransform(scaleX: easeScale, y: easeScale) }, completion: { (completed: Bool) -> Void in UIView.animate(withDuration: easingDuration, delay: 0.0, options: .curveEaseOut, animations: { () -> Void in self.transform = CGAffineTransform.identity }, completion: { (completed: Bool) -> Void in }) }) } /** Zoom out any view with specified offset magnification. - parameter duration: animation duration. - parameter easingOffset: easing offset. */ func zoomOutWithEasing(duration: TimeInterval = 0.2, easingOffset: CGFloat = 0.2) { let easeScale = 1.0 + easingOffset let easingDuration = TimeInterval(easingOffset) * duration / TimeInterval(easeScale) let scalingDuration = duration - easingDuration UIView.animate(withDuration: easingDuration, delay: 0.0, options: .curveEaseOut, animations: { () -> Void in self.transform = CGAffineTransform(scaleX: easeScale, y: easeScale) }, completion: { (completed: Bool) -> Void in UIView.animate(withDuration: scalingDuration, delay: 0.0, options: .curveEaseOut, animations: { () -> Void in self.transform = CGAffineTransform(scaleX: 0.0, y: 0.0) }, completion: { (completed: Bool) -> Void in }) }) } 

}

Swift 3版本:

  UIView.animate(withDuration: 0.6, animations: { button.transform = CGAffineTransform.identity.scaledBy(x: 0.6, y: 0.6) }, completion: { (finish) in UIView.animate(withDuration: 0.6, animations: { button.transform = CGAffineTransform.identity }) }) 

Swift 3.x +

 extension UIButton { func pulsate() { let pulse = CASpringAnimation(keyPath: "transform.scale") pulse.duration = 0.2 pulse.fromValue = 0.95 pulse.toValue = 1.0 pulse.autoreverses = true pulse.repeatCount = 2 pulse.initialVelocity = 0.5 pulse.damping = 1.0 layer.add(pulse, forKey: "pulse") } func flash() { let flash = CABasicAnimation(keyPath: "opacity") flash.duration = 0.2 flash.fromValue = 1 flash.toValue = 0.1 flash.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) flash.autoreverses = true flash.repeatCount = 3 layer.add(flash, forKey: nil) } func shake() { let shake = CABasicAnimation(keyPath: "position") shake.duration = 0.05 shake.repeatCount = 2 shake.autoreverses = true let fromPoint = CGPoint(x: center.x - 5, y: center.y) let fromValue = NSValue(cgPoint: fromPoint) let toPoint = CGPoint(x: center.x + 5, y: center.y) let toValue = NSValue(cgPoint: toPoint) shake.fromValue = fromValue shake.toValue = toValue layer.add(shake, forKey: "position") } } 

用法:

 myButton.flash() // myButton.pulsate() // myButton.shake() 

学分: 肖恩·艾伦

它和我一起工作如下,animation设置得很小,然后当它开始animation时,它恢复到原来的大小:

 button.transform = CGAffineTransformMakeScale(0.6, 0.6) UIView.animateWithDuration(0.3, animations: { () -> Void in button.transform = CGAffineTransformMakeScale(1,1) }) 

我更喜欢有新闻animation,并且比其他例子更快速地设置它,完成控制等待animation结束:

Swift 3

 extension UIButton { func press(completion:@escaping ((Bool) -> Void)) { UIView.animate(withDuration: 0.05, animations: { self.transform = CGAffineTransform(scaleX: 0.8, y: 0.8) }, completion: { (finish: Bool) in UIView.animate(withDuration: 0.1, animations: { self.transform = CGAffineTransform.identity completion(finish) }) }) } } 

用法

 @IBAction func playPauseBtnTap(_ sender: Any) { let playPauseBtn = sender as! UIButton playPauseBtn.press(completion:{ finish in if finish { print("animation ended") } } } 

使用下面的animationbutton将从其全尺寸开始,减小到0.6,弹簧animation弹回到全尺寸。

 [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:0.3 options:0 animations:^{ //Animations button.transform = CGAffineTransformIdentity; CGAffineTransformMakeScale(0.6, 0.6) } completion:^(BOOL finished) { //Completion Block [UIView.animateWithDuration(0.5){ button.transform = CGAffineTransformIdentity }]; }]; 

iOS 9和xCode 7

 //for zoom in [UIView animateWithDuration:0.5f animations:^{ self.sendButton.transform = CGAffineTransformMakeScale(1.5, 1.5); } completion:^(BOOL finished){}]; // for zoom out [UIView animateWithDuration:0.5f animations:^{ self.sendButton.transform = CGAffineTransformMakeScale(1, 1); }completion:^(BOOL finished){}]; 

这将带来一个美妙的弹跳效果:

 @IBAction func TouchUpInsideEvent(sender: UIButton) { UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: CGFloat(0.20), initialSpringVelocity: CGFloat(6.0), options: UIViewAnimationOptions.AllowUserInteraction, animations: { sender.transform = CGAffineTransformIdentity }, completion: { Void in() } ) } @IBAction func touchDownEvent(sender: UIButton) { UIView.animateWithDuration(0.15, animations: { sender.transform = CGAffineTransformMakeScale(0.6, 0.6) }) } 

如果你想要一个完成处理程序的自动反向效果,你可以试试这个。

 viewToAnimate.transform = CGAffineTransform(scaleX: 0.1, y: 0.1) UIView.animate(withDuration: 0.7, // your duration delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6.0, animations: { _ in viewToAnimate.transform = .identity }, completion: { _ in // Implement your awesome logic here. }) 

缩放button或三次或更多的任何视图使用下面的代码。 swift 3或者swift 4与xcode 9。

  UIView.animate(withDuration: 0.2, animations: { self.cartShowHideBtnView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) }, completion: { (finish: Bool) in UIView.animate(withDuration: 0.2, animations: { self.cartShowHideBtnView.transform = CGAffineTransform.identity }, completion:{(finish: Bool) in UIView.animate(withDuration: 0.2, animations: { self.cartShowHideBtnView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) }, completion: { (finish: Bool) in UIView.animate(withDuration: 0.2, animations: { self.cartShowHideBtnView.transform = CGAffineTransform.identity }, completion:{(finish: Bool) in UIView.animate(withDuration: 0.2, animations: { self.cartShowHideBtnView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) }, completion: { (finish: Bool) in UIView.animate(withDuration: 0.2, animations: { self.cartShowHideBtnView.transform = CGAffineTransform.identity }) }) }) }) }) })