gpt4 book ai didi

ios - 无法为类型 UIPanGestureRecognizer 调用初始值设定项

转载 作者:行者123 更新时间:2023-11-29 01:33:04 25 4
gpt4 key购买 nike

我正在学习有关手势驱动应用程序的教程,http://www.raywenderlich.com/77974/making-a-gesture-driven-to-do-list-app-like-clear-in-swift-part-1

然而,当我开始实现识别器时,我收到了错误

无法使用类型为“(target: NSObject -> () -> TableViewCell, action: String)”的参数列表调用类型“UIPanGestureRecognizer”的初始化程序

快速版本: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})
}
}
}
}

最佳答案

//添加一个 pan 识别器

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

教程说明

Open TableViewCell.swift and add the following code at the end of the overridden init method:

所以你应该这样做。您在错误的地方输入了代码。

关于ios - 无法为类型 UIPanGestureRecognizer 调用初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33254604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com