在一个视图的多姿态识别器

时间:2015-12-03 00:06:56

标签: ios swift

我的工作代码将窗口左侧的标签滑动,然后再向右滑动以完全显示。由于我学得很快,我很感激你的意见,以改善它。我提到了一些无效的因素。

工作代码:

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]
}
}

尝试改善后失败:

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)
    }
}
}

1 个答案:

答案 0 :(得分:0)

如何移动

parentView.gestureRecognizers = [gestureRecognizer]

viewDidLoad()