作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为三个 UIImageView
编写了一个 touchesMoved
函数,我想添加一个函数,让我使用多个 touchPoint 来移动 UIImageView
s,但我不太确定该怎么做,如果有一点帮助会很棒。
import UIKit
class ViewController: UIViewController {
var attachment: UIAttachmentBehavior?
var location = CGPoint(x: 0, y: 0)
@IBOutlet var Ball3: UIImageView!
@IBOutlet var Ball2: UIImageView!
@IBOutlet var Ball1: UIImageView!
@IBOutlet var Floor: UIImageView!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch2 = touches.first as UITouch!
let touch = touches.first as UITouch!
location = touch.locationInView(self.view)
location = touch2.locationInView(self.view)
if CGRectContainsPoint(Ball1.frame,location)
{
attachment = UIAttachmentBehavior(item: Ball1, attachedToAnchor: location)
animator!.addBehavior(attachment!)
}
if CGRectContainsPoint(Ball2.frame,location)
{
attachment = UIAttachmentBehavior(item: Ball2, attachedToAnchor: location)
animator!.addBehavior(attachment!)
}
if CGRectContainsPoint(Ball3.frame,location)
{
attachment = UIAttachmentBehavior(item: Ball3, attachedToAnchor: location)
animator!.addBehavior(attachment!)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch2 = touches.first as UITouch!
location = touch2.locationInView(self.view)
let touch = touches.first as UITouch!
location = touch.locationInView(self.view)
if attachment != nil
{
attachment!.anchorPoint = location
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if animator != nil && attachment != nil
{
self.attachment == nil
animator!.removeBehavior(self.attachment!)
}
}
最佳答案
如果你给每个球一个UIPanGestureRecognizer ,然后他们将独立运作。给定一个手势识别器,您可以使用它的 locationInView(_:)
或 translationInView(_:)
方法来获取偏移量(和 view
属性以获取它所附加的 View )。
我建议阅读 Event Handling Guide .
关于ios - 如何在 TouchesBegan Swift 中添加第二个接触点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34385676/
我是一名优秀的程序员,十分优秀!