2015-12-05 21 views
8

Swift: UIButton zaten çünküdöndürün UIButton 360 derece - Ben bunu ancak, benim UIButton 360 derece döner bir animasyon çalıştırmak çalışıyorlar

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

360 derece döndürün gelmez bu yer.

UIButton 360 derecemi nasıl değiştirebilirim?

cevap

22

Bir numara kullanabilirsiniz: önce 180 derece dönerek başlayıp 360 derece döndürün. Gecikmeli 2 animasyon kullanın. Bunu deneyin.

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) 

Umut bu Swift 3

+2

ŞAŞIRTICI !! Sadece öneri: düzgün bir 360 dönüşü sağlamak için, ikinci "animateWithDuration" için gecikme, animasyon süresinin yarısı kadar büyük olmalıdır. Çok teşekkür ederim! –

6

yardımcı olur:

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: Animasyon iç içe kapanma animasyon gecikme bloğu daha iyidir .

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

olarak ayrıca bir CAAnimation kullanabilirsiniz here tartışıldı. 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") 

Sen QuartzCore ithal etmek gerekecektir

Swift: Bu kod bir tam 360 dönüşünü uygular

import QuartzCore 

Ve ViewController CAAnimationDelegate uygun verilmiş olması gerekir:

class ViewController: UIViewController, CAAnimationDelegate { 

} 
+0

Bu doğru cevap! –

0

'yi kullanabilirsiniz.uzantısı (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) 
    } 
} 

Kullanımı:

self.vineTimeCapButton.rotate360Degrees() 

ya:

self. vineTimeCapButton.rotate360Degrees(duration:2) 
0

Swift 4 versiyonu aynı döndürmek için izin verir sonsuz saatleri görüntüle:

UIView.animate(withDuration: 0.5) { 
    self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi) 
} 
İlgili konular