使用CGPoint跟踪longpressgesture的确切位置

时间:2012-10-18 20:15:21

标签: iphone ios6 cgpoint

通过使用CGPoint位置,它总是在uiscrollview中保存最后一个图像。当我点击其他图像保存。我该怎么做才能保存我拍摄的精确图像。

UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;

NSInteger numberOfViews = 61;

for (int i = 0; i < numberOfViews; i++) {

    CGFloat xOrigin = i * self.view.frame.size.width;

 NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];

    _image = [UIImage imageNamed:imageName];

    _imageView = [[UIImageView alloc] initWithImage:_image];

    _imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);

 UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
                                                       initWithTarget:self
                                                       action:@selector(handleLongPress:)];

    imageScrollView.userInteractionEnabled = YES;
    [imageScrollView addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.delegate = self;
    [gestureRecognizer release];

    [imageScrollView addSubview:_imageView];
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

    - (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Save Photo", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    [actionSheet showInView:self.view];
    [actionSheet release];

     }}

 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

switch (buttonIndex) {
    case 0:
        [self savePhoto];

       break;

    default:
        break;

}

   -(void)savePhoto{

CGPoint location = [gesture locationInView:_imageView];

if  (CGRectContainsPoint(_imageView.bounds, location)){

UIImageWriteToSavedPhotosAlbum(_image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
   }}}

任何想法都将受到赞赏。

由于

1 个答案:

答案 0 :(得分:1)

该点始终显示在UIScrollView被触发的LongPressGestureRecognizer范围内。您应该检查滚动视图的contentOffset(使用contentOffset.x进行水平布局,contentOffset.y进行垂直布局)以检测应保存的图像。

此外,您可以将触摸点转换为UIImageView实例的本地坐标系,并查看该点是否位于图像视图的bounds矩形内。

<强>更新

例如,你可以使用这样的东西来检测点是否在图像视图的边界内(注意:我已经没有测试了这个,这假设添加了多个图像视图滚动视图):

if (CGRectContainsPoint(_imageView.bounds, [self.view convertPoint:location toView:_imageView]))
{
    // do something
}

您还应考虑在向用户显示UIActionSheet之前检测应保存哪个图像并存储对该图像的引用,因为这可能会减少您可能遇到的潜在问题的数量,并且更易于阅读之后,这是我的主观意见。