无法为类型UIPanGestureRecognizer

时间:2015-10-21 08:39:16

标签: ios swift uipangesturerecognizer

我关注手势驱动应用的本教程http://www.raywenderlich.com/77974/making-a-gesture-driven-to-do-list-app-like-clear-in-swift-part-1

然而,当我来实现识别器时,我收到错误

Cannot invoke initializer for type 'UIPanGestureRecognizer' with an argument list of type '(target: NSObject -> () -> TableViewCell, action: String)'

Swift版本: Apple Swift 2.0版(swiftlang-700.0.59 clang-700.0.72) 目标:x86_64-apple-darwin14.5.0

import UIKit
import QuartzCore

class TableViewCell: UITableViewCell {

    var originalCenter = CGPoint()
    var deleteOnDragRelease = false

    let gradientLayer = CAGradientLayer()

    required init(coder aDecoder: NSCoder) {
        fatalError("NSCoding not supported")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        // gradient layer for cell
        gradientLayer.frame = bounds
        let color1 = UIColor(white: 1.0, alpha: 0.2).CGColor as CGColorRef
        let color2 = UIColor(white: 1.0, alpha: 0.1).CGColor as CGColorRef
        let color3 = UIColor.clearColor().CGColor as CGColorRef
        let color4 = UIColor(white: 0.0, alpha: 0.1).CGColor as CGColorRef
        gradientLayer.colors = [color1, color2, color3, color4]
        gradientLayer.locations = [0.0, 0.01, 0.95, 1.0]
        layer.insertSublayer(gradientLayer, atIndex: 0)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = bounds
    }

    // add a pan recognizer
    var recognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
    recognizer.delegate = self
    addGestureRecognizer(recognizer)

    //MARK: - horizontal pan gesture methods
    func handlePan(recognizer: UIPanGestureRecognizer) {
        // 1
        if recognizer.state == .Began {
            // when the gesture begins, record the current center location
            originalCenter = center
        }
        // 2
        if recognizer.state == .Changed {
            let translation = recognizer.translationInView(self)
            center = CGPointMake(originalCenter.x + translation.x, originalCenter.y)
            // has the user dragged the item far enough to initiate a delete/complete?
            deleteOnDragRelease = frame.origin.x < -frame.size.width / 2.0
        }
        // 3
        if recognizer.state == .Ended {
            // the frame this cell had before user dragged it
            let originalFrame = CGRect(x: 0, y: frame.origin.y,
                width: bounds.size.width, height: bounds.size.height)
            if !deleteOnDragRelease {
                // if the item is not being deleted, snap back to the original location
                UIView.animateWithDuration(0.2, animations: {self.frame = originalFrame})
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

"files.exclude": { "**/*.meta": true }

从代码的这一点开始,您将在类的主体内部而不是在特定的方法中编写。

教程陈述

  

打开TableViewCell.swift并在重写的init方法结尾处添加以下代码

所以你应该这样做。您已将代码输入错误的位置。