![](/img/trans.png)
[英]How do I use UILongPressGestureRecognizer with a UICollectionViewCell in Swift?
[英]How do I end UILongPressGestureRecognizer with an animation?
我使用UILongPressGestureRecognizer
為UIImageView設置了動畫, UILongPressGestureRecognizer
所示:
override func viewDidAppear(animated: Bool) {
var longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPress"))
imageView.addGestureRecognizer(longPress)
imageView.userInteractionEnabled = true
}
func longPress()
{
let bounds = self.imageView.bounds
UIView.animateWithDuration(0.5, animations: {
self.imageView.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 22, height: bounds.size.height + 22)
})
println("user pressed on image")
}
動畫只會導致UIImageview的高度和寬度在longPress上展開,但是當釋放按下時,邊界會繼續增長。當釋放按下時,如何將UIImageView的邊界返回到原點寬度和高度?
嘗試這個:
var oldbounds:CGRect!
@IBOutlet var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
var longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPress:"))
longPress.minimumPressDuration = 0.01
image.addGestureRecognizer(longPress)
image.userInteractionEnabled = true
}
func longPress(gesture:UILongPressGestureRecognizer)
{
if gesture.state == UIGestureRecognizerState.Began
{
oldbounds = self.image.bounds
}
else if gesture.state == UIGestureRecognizerState.Changed
{
let bounds = self.image.bounds
UIView.animateWithDuration(0.5, animations: {
self.image.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 22, height: bounds.size.height + 22)
})
println("user pressed on image")
}
else
{
let bounds = self.image.bounds
UIView.animateWithDuration(0.5, animations: {
self.image.bounds = self.oldbounds
})
println("user release on image")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.