简体   繁体   English

在一个视图上的多手势识别器

[英]Multi gesture recognizer on one view

My working code slides a label of to the left of the window and slide it again to the right to show it fully. 我的工作代码将标签滑动到窗口的左侧,然后再次将其滑动到右侧以完全显示它。 Since I am learning swift, I would appreciate your comments to improve on it. 由于我正在学习快速,因此,感谢您提出的改进意见。 I included some factoring which failed to work. 我包括一些无法正常工作的因素。

Working code: 工作代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myLabel: UILabel!
@IBOutlet var parentView: UIView!
var swipGestureRight: UISwipeGestureRecognizer!
var swipGestureLeft: UISwipeGestureRecognizer!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    myLabel.userInteractionEnabled = true

    swipGestureLeft = UISwipeGestureRecognizer(target: self, action: Selector("swipViewLeft:"))
    swipGestureLeft.direction = .Left
    parentView.gestureRecognizers = [swipGestureLeft]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func swipViewLeft(gesture: UISwipeGestureRecognizer){
    UIView.animateWithDuration(0.3, animations: {self.myLabel.center.x -= (self.myLabel.bounds.width)}, completion: nil)
    swipGestureRight = UISwipeGestureRecognizer(target: self, action: Selector("swipViewRight:"))
    swipGestureRight.direction = .Right
    parentView.gestureRecognizers = [swipGestureRight]
}

func swipViewRight(gesture: UISwipeGestureRecognizer){
    UIView.animateWithDuration(0.3, animations: {self.myLabel.center.x += (self.myLabel.bounds.width)}, completion: nil)
    swipGestureLeft = UISwipeGestureRecognizer(target: self, action: Selector("swipViewLeft:"))
    swipGestureLeft.direction = .Left
    parentView.gestureRecognizers = [swipGestureLeft]
}
}

Failed after trying to improve: 尝试改进后失败:

import UIKit
class ViewController: UIViewController {

@IBOutlet weak var myLabel: UILabel!
@IBOutlet var parentView: UIView!
var swipGestureRecognizer: UISwipeGestureRecognizer!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    myLabel.userInteractionEnabled = true

    swipGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipping:"))

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func swipping(gestureRecognizer: UISwipeGestureRecognizer){
    if gestureRecognizer.direction == .Left {
        parentView.gestureRecognizers = [gestureRecognizer]
        UIView.animateWithDuration(0.3, animations: {self.myLabel.center.x -= (self.myLabel.bounds.width)}, completion: nil)
    } else {
        UIView.animateWithDuration(0.3, animations: {self.myLabel.center.x += (self.myLabel.bounds.width)}, completion: nil)
    }
}
}

How about move 怎么样

parentView.gestureRecognizers = [gestureRecognizer]

to viewDidLoad() . 查看viewDidLoad()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM