[英]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.