简体   繁体   English

查找相同的UIImageView类是否相交

[英]find if same UIImageView class intersects each other

I created a subclass of UIimageView because i want to use this imageview multiple times. 我创建了UIimageView的子类,因为我想多次使用此imageview。 I'm using touchesmoved to drag the image. 我正在使用touchesmoved拖动图像。 So when i use multiple images of same class, i want to find whether they intersect each other. 因此,当我使用同一类的多个图像时,我想确定它们是否彼此相交。 Here's the image class code 这是图像类代码

 import UIKit

    class imgBall: UIImageView {

        private var xOffset: CGFloat = 0.0
        private var yOffset: CGFloat = 0.0

        required init(coder aDecoder:NSCoder) {
            fatalError("use init(point:")
        }


        init(point:CGPoint) {
            let image = UIImage(named: "ball.png")!
            super.init(image:image)

            self.frame = CGRect(origin: point, size: CGSize(width: 100, height: 100))
            self.userInteractionEnabled = true
        }

        override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
            let point = touches.anyObject()!.locationInView(self.superview)
            xOffset = point.x - self.center.x
            yOffset = point.y - self.center.y

        }

        override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
            let point = touches.anyObject()!.locationInView(self.superview)
            self.center.x = point.x - xOffset        
        }
    }

and this is how I'm using it in gameviewcontroller 这就是我在gameviewcontroller中使用它的方式

viewDidLoad() {

var ball1 = imgBall(point: CGPoint(x: 20, y: 0))
self.view.addSubview(ball1)

var ball2 = imgBall(point: CGPoint(x: 50, y: 70))
self.view.addSubview(ball2)

} }

so how do i find out if they intersect/collide each other? 那么我如何确定它们是否相交/碰撞?

You've got to use CGRectIntersectsRect with something like this : 您必须将CGRectIntersectsRect与以下内容配合使用:

if (CGRectIntersectsRect(ball1.frame,ball2.frame) {
    // they intersect
} else {
    // they're safe
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改其他类的UIImageView - Change UIImageView of other class 如何检查NSMutableDictionary中的UIImageView是否与同一NSMutableDictionary中的另一个UIImageView相交? - How do you check if a UIImageView in an NSMutableDictionary Intersects with another UIImageView in same NSMutableDictionary? UIImageView图像彼此重叠 - UIImageView images overlaps each other 如何检测触摸输入是否与uiimageview相交 - How to detect if Touch input intersects uiimageview 在数组中查找具有相同颜色的元素,如果元素彼此相邻,则显示true - Find elements in array with the same color and print true if they are next to each other 自定义类的单独实例彼此具有相同的值 - Separate instances of custom class have the same values as each other UIScrollView 多个 UIView/UIImageView 相互堆叠用于绘图 - UIScrollView with multiple UIView/UIImageView stacked on each other for drawing 如何访问其他控制器类中的UIImageView子类中声明的图像的属性? - How to access the properties of image declared in subclass of UIImageView in other controller class? 将相同的UIImageView放入两个不同的类屏幕(iOS) - Getting same UIImageView onto two differant class screens (iOS) 如何为单击按钮时以编程方式创建的 UIImageView 设置动画? (每个按钮单击都会创建一个具有相同图像的新 UIImageView) - How do I animate a UIImageView that was created programatically upon button click? (Each button click creates a new UIImageView with the same image)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM