【问题标题】:problem in calling function调用函数的问题
【发布时间】:2011-10-25 15:01:18
【问题描述】:

我有一个 UIView,它有一个 UIScrollView 作为子视图,而它又有一个 UIImageView 作为它的子视图。我也可以缩放。但我想在双击时调用另一个函数。我正在使用此代码:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view 
{
     if(view == scrollView)
     {
        UITouch *touch = [touches anyObject];
        if([touch tapCount]== 2)
        {
           [self setViewForProductDispaly];
           return YES;
        }

      }
 return NO;

}

当我点击它时,上面的方法没有被调用。这可能是什么原因。

我的滚动视图

scrollView.hidden=NO;
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0,self.bounds.size.width,self.bounds.size.height )];

scrollView.maximumZoomScale = 3.0;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
scrollView.delegate =self;
scrollView.bouncesZoom = YES;
scrollView.delaysContentTouches=NO;


bigImageView.autoresizesSubviews = YES;
bigImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
bigImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, bigImg.size.width, bigImg.size.height)];
bigImageView.image = bigImg;
bigImageView.userInteractionEnabled = YES;

[scrollView addSubview:bigImageView];
[bigImageView release];

[self addSubview:scrollView];
[scrollView release];

【问题讨论】:

    标签: iphone uiview uiscrollview


    【解决方案1】:

    最好将 UIGestureRecogizers 用于双击等基本手势:

    UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
                            initWithTarget:self action:@selector(handleDoubleTap:)];
    
    doubleTapGestureRecognizer.numberOfTapsRequired = 2;
    [self.scrollView addGestureRecognizer:doubleTapGestureRecognizer];
    

    在您的handleDoubleTap: 函数中,您可以调用任何您想要的方法。

    【讨论】:

      【解决方案2】:

      看起来 bigImageView 在添加到它时正在拉伸以适应 scrollView。所以,您的方法没有被调用,因为您实际上是在点击 bigImageView,而不是 scrollView。

      【讨论】:

      • 这也是我的第一个猜测。您可以通过在整个代码中放置 NSLog 语句来简单地验证这一点,以查看正在执行的内容。试试:if(view == bigImageView)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 2021-06-09
      • 2015-11-17
      • 2018-05-24
      • 2012-06-29
      • 1970-01-01
      相关资源
      最近更新 更多