我想连接一个动作,如果手势是一个轻击,它会以特定方式动画一个对象,但如果按下持续时间超过.5秒,它会做其他事情.
现在,我只是把动画连接起来了.我不知道如何区分长按和水龙头?如何访问印刷机持续时间以实现上述目标?
@IBAction func tapOrHold(sender: AnyObject) {
UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
self.polyRotate.transform = CGAffineTransformMakeRotation(1/3 * CGFloat(M_PI * 2))
})
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
self.polyRotate.transform = CGAffineTransformMakeRotation(2/3 * CGFloat(M_PI * 2))
})
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
self.polyRotate.transform = CGAffineTransformMakeRotation(3/3 * CGFloat(M_PI * 2))
})
}, completion: { (Bool) in
let vc : AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("NextView")
self.showViewController(vc as UIViewController, sender: vc)
})
Run Code Online (Sandbox Code Playgroud) 我试图检测手指何时第一次接触 SwiftUI 中的视图。我可以用 UIKit Events 很容易地做到这一点,但在 SwiftUI 中无法解决这个问题。
我已经尝试了最小移动为 0 的 DragGesture,但在您的手指移动之前它仍然不会改变。
TapGesture 仅在您抬起手指时才起作用,并且无论我将参数设置为什么,LongPressGesture 都不会足够快地触发。
DragGesture(minimumDistance: 0, coordinateSpace: .local).onChanged({ _ in print("down")})
LongPressGesture(minimumDuration: 0.01, maximumDistance: 100).onEnded({_ in print("down")})
Run Code Online (Sandbox Code Playgroud)
我想在手指与视图接触后立即检测 touchDown 事件。Apple 的默认手势对距离或时间都有限制。
更新:这不再是问题,因为 Apple 似乎更新了 DragGesture 的工作方式,或者我可能遇到了特定的上下文错误。
(对于SwiftUI,不是普通的UIKit)非常简单的示例代码,例如,在灰色背景上显示红色框:
struct ContentView : View {
@State var points:[CGPoint] = [CGPoint(x:0,y:0), CGPoint(x:50,y:50)]
var body: some View {
return ZStack {
Color.gray
.tapAction {
// TODO: add an entry to self.points of the location of the tap
}
ForEach(self.points.identified(by: \.debugDescription)) {
point in
Color.red
.frame(width:50, height:50, alignment: .center)
.offset(CGSize(width: point.x, height: point.y))
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我假设不是需要tapAction,而是需要TapGesture或其他东西?但即使在那儿,我也看不到任何方法来获取有关水龙头位置的信息。我将如何处理?