使用CGPoint跟踪longpressgesture的确切位置 [英] Tracing the exact location of the longpressgesture with CGPoint

查看:151
本文介绍了使用CGPoint跟踪longpressgesture的确切位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

By using CGPoint location it is always saving the last image in uiscrollview. When i m tapping on other image to save. What can i do to save the exact image one i tapped.

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);
   }}}

任何想法都将受到赞赏。

Any ideas will be appreciated.

谢谢

推荐答案

该点始终出现在 UIScrollView的范围内,其中触发了 LongPressGestureRecognizer 。您应检查滚动视图的 contentOffset (对于水平布局使用 contentOffset.x ,并使用 contentOffset。 y 用于垂直布局)以检测应保存的图像。

The point will always appear within the bounds of the UIScrollView in which the LongPressGestureRecognizer is triggered. You should check your scroll view's contentOffset (use contentOffset.x for horizontal layouts and contentOffset.y for vertical layouts) to detect which image you should save.

此外,您可以将触摸点转换为 UIImageView 实例的本地坐标系,看看该点是否位于图像视图的边界 rect。

Additionally you could convert the touch point to the UIImageView instance's local coordinate system and see if the the point lies within the image view's bounds rect.

UPDATE

例如,您可以使用类似的东西来检测点是否在图像视图的边界内(注意:I让对此进行测试,这假设滚动视图中添加了多个图像视图):

For example you could use something like this to detect if the point is within the image view's bounds (note: I have not tested this and this is assuming there is more than one image view added to the scroll view):

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

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

You should also consider detecting which image should be saved before and storing a reference to that image before displaying the UIActionSheet to the user as it may decrease the number of potential issues you might encounter and will be easier to read later, but this is my subjective opinion.

这篇关于使用CGPoint跟踪longpressgesture的确切位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆