当UIImageView点击屏幕上的某些点时,如何让事情发生?

时间:2015-05-25 23:05:27

标签: ios swift uiimageview

我有一个UIImageView用户可以拖动。我希望在屏幕上的某些点(被x: 0 y: 0x: 0 y: 100之后的所有点)点击(被拖过)时会发生一些事情。

这是我目前使用的代码:

@IBOutlet var imageView: UIImageView!

var location = CGPoint(x: 0, y: 0)

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    var touch : UITouch! = touches.first as! UITouch
    location = touch.locationInView(self.view)
    imageView.center = location
}

func pointDetection() {
    if location == CGPoint(x: 0, y: 100) {
        println("1 Point!")
    } else {
        println("0 Points")
    }
}

2 个答案:

答案 0 :(得分:0)

您在问题中提到“(从x: 0 y: 0x: 0 y: 100的所有点,但您的if声明仅为true一个具体点,CGPoint(x: 0, y: 100)

将其更改为:

if location.y >= 0.0 && location.y <= 100.0 {
    println("1 Point!")
} else {
    println("0 Points")
}

我也没看到你在哪里调用pointDetection()功能。将其添加到touchesMoved

答案 1 :(得分:0)

你是说这条线而不是指向?每次检查前一行和新点是否位于您行的两侧:

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];

    CGPoint nowPoint = [[touches anyObject] locationInView:self.view];
    CGPoint prevPoint = [[touches anyObject] previousLocationInView:self.view];

    CGRect rect = CGRectMake(nowPoint.x, nowPoint.y, nowPoint.x + prevPoint.x, nowPoint.y + prevPoint.y);
    // then check if line intersects a rectangle
}

Here is example of intersects function