gpt4 book ai didi

ios - 如何在 TouchesBegan Swift 中添加第二个接触点

转载 作者:行者123 更新时间:2023-11-28 06:53:10 28 4
gpt4 key购买 nike

我为三个 UIImageView 编写了一个 touchesMoved 函数,我想添加一个函数,让我使用多个 touchPoint 来移动 UIImageViews,但我不太确定该怎么做,如果有一点帮助会很棒。

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/

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