如何制作 UIImageView "flickable"?

How do I make a UIImageView "flickable"?

我有一个 UIImageView 用户可以四处移动,但我希望用户可以轻扫它或 "flick" 它朝一个方向飞,它会飞向用户的方向swiped/flicked它。

这是 UIImageView 的代码:

 @IBOutlet var imageView: UIImageView!

var location = CGPoint(x: 0, y: 0)

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

    var touch : UITouch! = touches.first as! UITouch

    location = touch.locationInView(self.view)

    imageView.center = location


}

你可以使用UISwipeGestureRecognizer移动
这是一个代码

 @IBOutlet var imageView: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()

    var rightSwipe = UISwipeGestureRecognizer(target: self, action: "HandleSwipeGesture:")
    var leftswipe = UISwipeGestureRecognizer(target: self, action: "HandleSwipeGesture:")
    rightSwipe.direction = .Right
    leftswipe.direction = .Left
    imageView.addGestureRecognizer(rightSwipe)
    imageView.addGestureRecognizer(leftswipe)
}


 func HandleSwipeGesture(sender:UISwipeGestureRecognizer){

        if(sender.direction == .Right){
            // do other task
        }
        else{
            if(sender.direction == .Left){
             // do other task
            }
        }

}

滑动可以试试

varysLeftSwipe=UISwipeGestureRecognizer(target:self,action:Selector("handleSwipes:"))

var ysRightSwipe = UISwipeGestureRecognizer(目标:自我,动作:选择器("handleSwipes:"))

    ysLeftSwipe.direction = UISwipeGestureRecognizerDirection.Left
    ysRightSwipe.direction = UISwipeGestureRecognizerDirection.Right

    ysSlideImageView.addGestureRecognizer(ysLeftSwipe)
    ysSlideImageView.addGestureRecognizer(ysRightSwipe)

func handleSwipes(发件人:UISwipeGestureRecognizer){

    if (sender.direction == .Left) {

        self.showNextData()
    }

    if (sender.direction == .Right) {

        self.showPreviousData()
    }
}

func showPreviousData(){

    if index > 0 {

        index = index - 1
        ysForWordButton.hidden = false

        if index == 0{

            ysBackWordButton.hidden = true
        }
    }
    else{

        ysBackWordButton.hidden = true
        ysForWordButton.hidden = false

    }
    ysSlideImageView.image = UIImage(data: NSData(contentsOfURL: NSURL(string: ysCollectionImgArray.objectAtIndex(index) as NSString)!)!)
}


func showNextData(){

    if index < ysCollectionImgArray.count - 1 {

        index = index + 1
        ysBackWordButton.hidden = false

        if ysCollectionImgArray.count - 1 == index {

            ysForWordButton.hidden = true
        }
    }
    else{

        ysForWordButton.hidden = true
        ysBackWordButton.hidden = false
        //            index = 0
    }

    ysSlideImageView.image = UIImage(data: NSData(contentsOfURL: NSURL(string: ysCollectionImgArray.objectAtIndex(index) as NSString)!)!)
}

您需要使用 UIKit Dynamics。

这里有一个教程: http://www.raywenderlich.com/71828/uikit-dynamics-tutorial-tossing-views