iOS检测点是否为rect

时间:2013-08-16 23:36:18

标签: ios uiview uiimageview cgrect cgpoint

当用户可以从主视图中选择一个项目并将其拖到详细视图并放置它时,我正在使用分割视图控制器。当用户拖动时,UIView会出现在详细视图的底部,如果他们在拖动中间改变他们的想法并想要删除它,他们可以拖动该项目。我正在检测触摸是否在UIView中移除时遇到麻烦。以下是我到目前为止的情况:

当用户选择主控制器中的项目时执行的代码。

MasterViewController:

    // Create pointer to window
    UIWindow *window = [UIApplication sharedApplication].delegate.window;

    // Create draggable view
    self.draggableImageView = [[UIImageView alloc] initWithImage:imageView.image];

    // Adapt to orientation
    [self.draggableImageView rotateToOrientation:[[UIApplication sharedApplication] statusBarOrientation]];

    // Hide detail calloutview if there is one
    [self.detailViewController hideCalloutView:NO];

    // Add drag-view to window
    CGPoint location = [gesture locationInView:gesture.view.window];
    [self.draggableImageView setCenter:location];
    [window addSubview:[self draggableImageView]];

    // Animate the marker to jump up
    [UIView animateWithDuration:0.10
                          delay:0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{

                         // Move the marker up
                         [self.draggableImageView setCenter:CGPointMake(location.x + 75.0f, location.y)];

    } completion:^(BOOL finished) {

    }];

    // Show our remove marker view
    [self.detailViewController showRemoveMarkerView];

在用户拖动时,使用更新的点调用详细视图控制器中的方法,这是该方法中的代码:

- (void)pointForMarkerDidChange:(UIImageView *)imageView atPoint:(CGPoint)point
{
    NSLog(@"Point for Marker Did Change, point = %@", NSStringFromCGPoint(CGPointMake(point.x - 75.0f, point.y)));

    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    CGRect removeMarkerFrame = [window convertRect:self.removeMarkerView.frame fromView:self.view];

    if (CGRectContainsPoint(removeMarkerFrame, point)) {

        NSLog(@"YES");
    }
}

编辑:我更改了以下代码,现在可以使用了。

- (void)pointForMarkerDidChange:(UIImageView *)imageView atPoint:(CGPoint)point
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    CGRect removeMarkerFrame = [self.removeMarkerView convertRect:self.removeMarkerView.round.frame toView:self.view];
    CGPoint point2 = [window convertPoint:point toView:self.view];

    if (CGRectContainsPoint(removeMarkerFrame, point2)) {

        NSLog(@"YES");
    }
}

0 个答案:

没有答案