1

有一些与此类似的帖子,但我无法将其他响应调整为我的代码。我在我touchesBegan的.touchesMovedtouchesEndedscrollview

我目前有一个大的scrollview(3072 x 2304)。我有一个UIImageView这样subview的并希望检测到任何一个的触摸(没关系,因为它会为我产生相同的结果)。

这是我目前的处理方法:

 //Set scrollview to size of image
self.myScrollView.contentSize = CGSizeMake(3072, 2304);

//Load image in big rect
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 3072, 2304)];

//set image to pic in bundle
myImageView.image = [UIImage imageNamed:@"dot.jpg"];

//make imageview subview of scrollview
[self.myScrollView addSubview: myImageView];

//Making subview for touches
CGRect frame  = CGRectMake(0, 0, 3072, 2304);
touchView = [[UIScrollView alloc] initWithFrame:frame];
[self.myScrollView addSubview: touchView];

//Allow for 2 touches to move screen
for (UIGestureRecognizer *gestureRecognizer in myScrollView.gestureRecognizers)
{
    if ([gestureRecognizer  isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *panGR = (UIPanGestureRecognizer *) gestureRecognizer;
        panGR.minimumNumberOfTouches = 2;            
    }
} 

笔记:

建议使用手势识别器的方法并不理想。我知道,别无选择touchesMoved;实现这一点的唯一可行方法是子类化 UIScrollView,但这并没有给我带来任何乐趣。

4

1 回答 1

0

尝试使用这个。你知道imageview的用户交互是默认的false所以你需要设置它true

myImageView.userInteractionEnabled = YES;

你需要在 viewDidLoad 中为你的滚动视图添加手势

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    gestureRecognizer.delegate = self;
    [scrollView addGestureRecognizer:gestureRecognizer];
}

-(void) handleGesture:(UIGestureRecognizer *) sender
{
//------- do here 
}
于 2013-05-17T10:40:26.130 回答