ios - 一个 View 上的多手势识别器

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

最佳答案

搬家怎么样

parentView.gestureRecognizers = [gestureRecognizer]

viewDidLoad()

关于ios - 一个 View 上的多手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34055529/

相关文章:

ios - 如何通过调用方法设置导航栏颜色?

ios - 当应用程序处于后台状态 Swift 时暂停计时器

ios - 使用相同的 ViewController 类从同一 Storyboard调用第二个 View

ios - 混合 Box2D 加速度计 + 重力?

ios - 什么是FNFPlayer?它从后台线程访问UIApplication applicationState

ios - 适用于 iOS13.1.2 (17A860) 的 DeviceSupport 文件

ios - 如何删除位于可见区域之外的图 block 图层 Google map SDK for iOS

swift - 有没有办法对闭包进行编码和解码?

ios - 使用 Swift 和 UITableView 实现表头 View 的正确方式

ios - 如何从我的 NetworkingService 响应访问我的 HomeViewController 中的用户属性?