2015-12-05 24 views
8

Tôi đã cố gắng để chạy một hình ảnh động mà quay độ UIButton 360 của tôi, tuy nhiên khi tôi làm điều này:Xoay UIButton 360 độ - Swift

UIView.animateWithDuration(3.0, animations: { 
    self.vineTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2)) 
    self.instagramTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2)) 
    }) 

Nó không xoay 360 độ vì UIButton đã ở vị trí đó.

Làm cách nào để xoay 360 độ UIButton của tôi?

Trả lời

22

Bạn có thể sử dụng mẹo: bắt đầu xoay 180 độ trước và sau đó xoay với 360 độ. Sử dụng 2 hoạt ảnh có độ trễ. Hãy thử điều này.

UIView.animateWithDuration(0.5) {() -> Void in 
    button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) 
} 

UIView.animateWithDuration(0.5, delay: 0.45, options: UIViewAnimationOptions.CurveEaseIn, animations: {() -> Void in  
    button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 2)) 
}, completion: nil) 

Swift 4

UIView.animate(withDuration: 0.5) {() -> Void in 
    self.settingsButton.transform = CGAffineTransform(rotationAngle: CGFloat.pi) 
} 

UIView.animate(withDuration: 0.5, delay: 0.45, options: UIViewAnimationOptions.curveEaseIn, animations: {() -> Void in 
    self.settingsButton.transform = CGAffineTransform(rotationAngle: CGFloat.pi * 2.0) 
}, completion: nil) 

Hope this helps

+2

BẠN ĐANG TUYỆT VỜI !! Chỉ đề xuất: để làm cho xoay 360 độ mượt mà, độ trễ cho "animateWithDuration" thứ hai cần phải bằng một nửa kích thước của thời lượng hoạt ảnh. Cảm ơn bạn rất nhiều !! –

6

trong Swift 3:

UIView.animate(withDuration:0.5, animations: {() -> Void in 
    button.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI)) 
}) 

UIView.animate(withDuration: 0.5, delay: 0.45, options: .curveEaseIn, animations: {() -> Void in 
    button.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI * 2)) 
}, completion: nil) 
1

Swift 4: Animation lồng nhau đóng cửa là tốt hơn so với khối trì hoãn hoạt hình .

UIView.animate(withDuration: 0.5, animations: { 
    button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))) 
}) { (isAnimationComplete) in 

     // Nested Block 
UIView.animate(withDuration: 0.5) { 
    button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi * 2))) 
     }  
} 
2

Như đã thảo luận here, bạn cũng có thể sử dụng CAAnimation. Mã này áp dụng một đầy đủ 360 xoay:

Swift 3

let fullRotation = CABasicAnimation(keyPath: "transform.rotation") 
fullRotation.delegate = self 
fullRotation.fromValue = NSNumber(floatLiteral: 0) 
fullRotation.toValue = NSNumber(floatLiteral: Double(CGFloat.pi * 2)) 
fullRotation.duration = 0.5 
fullRotation.repeatCount = 1 
button.layer.add(fullRotation, forKey: "360") 

Bạn sẽ cần phải nhập khẩu QuartzCore:

import QuartzCore 

Và ViewController của bạn cần phải phù hợp với CAAnimationDelegate:

class ViewController: UIViewController, CAAnimationDelegate { 

} 
+0

Đây là câu trả lời đúng! –

0

Bạn có thể sử dụng UIView mở rộng (Swift 3.x):

extension UIView { 
    func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) { 
     let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") 
     rotateAnimation.fromValue = 0.0 
     rotateAnimation.toValue = CGFloat(.pi * 2.0) 
     rotateAnimation.duration = duration 

     if let delegate: AnyObject = completionDelegate { 
      rotateAnimation.delegate = delegate as? CAAnimationDelegate 
     } 
     self.layer.add(rotateAnimation, forKey: nil) 
    } 
} 

Cách sử dụng:

self.vineTimeCapButton.rotate360Degrees() 

hay:

self. vineTimeCapButton.rotate360Degrees(duration:2) 
0

Swift 4 phiên bản đó cho phép bạn xoay cùng xem thời gian vô hạn:

UIView.animate(withDuration: 0.5) { 
    self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi) 
} 
Các vấn đề liên quan