【问题标题】:Tracing the exact location of the longpressgesture with CGPoint用 CGPoint 追踪长按手势的准确位置
【发布时间】:2012-10-18 20:15:20
【问题描述】:

通过使用 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);
   }}}

任何想法都会受到赞赏。

谢谢

【问题讨论】:

    标签: iphone ios6 cgpoint


    【解决方案1】:

    该点将始终出现在触发LongPressGestureRecognizerUIScrollView 的范围内。您应该检查滚动视图的contentOffset(水平布局使用contentOffset.x,垂直布局使用contentOffset.y)来检测您应该保存哪个图像。

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

    更新

    例如,您可以使用类似的方法来检测该点是否在图像视图的范围内(注意:我没有对此进行了测试,这是假设添加了多个图像视图滚动视图):

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

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

    【讨论】:

    • 当图像被放置在 UIScrollView 的单行中时,如何将其放入代码中
    • 在滚动视图中保存最后一张图片时仍然没有同样的问题
    • 当用户滚动图片时,您是否更改了_image ivar 所引用的图片?如果没有其他代码触及引用,那么它将始终是存储在其中的最后一个东西,从上面的代码示例来看,它看起来是初始化视图层次结构的 for 循环。同样,您还需要确保在用户滚动浏览内容时更新 _imageView ivar。
    • 如果我可以用 [imageView setTag:i] 标记它们会怎样?稍后使用 [scrollView viewWithTag:index+1] 检索它们;这是有道理的
    • 让你的UIViewController在头部(.h)中采用UIScrollViewDelegate协议,将你的视图控制器设置为滚动视图的委托,然后在实现中实现– scrollViewDidScroll:方法(.m )。在scrollViewDidScroll: 方法中,检查滚动视图的contentOffset 属性并查看用户滚动了多少“页面”以检测当前显示的图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    相关资源
    最近更新 更多