跟踪与CGPoint的longpressgesture的确切位置(Tracing the exact

2019-08-01 15:51发布

通过使用CGPoint位置它总是节省UIScrollView中的最后一个图像。 当IM攻上其他的图像进行保存。 我能做些什么来拯救我拍了拍确切的图像之一。

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

任何想法将不胜感激。

谢谢

Answer 1:

该点将总是出现在允许的范围内UIScrollView其中LongPressGestureRecognizer被触发。 您应该检查你的滚动视图contentOffset (使用contentOffset.x卧式布局和contentOffset.y对于垂直布局)来检测你应该保存它的形象。

另外,你可以在触摸点转换为UIImageView实例的局部坐标系,看看该点位于图像视图的内部bounds矩形。

UPDATE

例如,你可以使用类似这样来检测,如果该点位于图像视图的范围内(注:我没有测试过这一点,这是假设有一个以上的图像视图中添加滚动视图):

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

你也应该考虑检测其图像应保存前,显示之前存储到该图像的参考UIActionSheet给用户,因为它可能会降低你可能遇到的潜在问题的数量和将来就更容易阅读,但是这是我的主观意见。



文章来源: Tracing the exact location of the longpressgesture with CGPoint