我有一个UIImageView
用户可以四处移动,但我希望用户可以轻扫它或者向某个方向“轻弹”它,它会朝着用户滑动/轻弹的方向飞行
以下是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
}
答案 0 :(得分:0)
你可以使用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
}
}
}
答案 1 :(得分:0)
varysLeftSwipe = UISwipeGestureRecognizer(目标:自,动作:选择器( “handleSwipes:”))
var ysRightSwipe = UISwipeGestureRecognizer(target:self,action:Selector(“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)!)!)
}
答案 2 :(得分:0)
您需要使用UIKit Dynamics。
这是一个教程: http://www.raywenderlich.com/71828/uikit-dynamics-tutorial-tossing-views