Ios UIScrollView中的触摸交互
有一些帖子与此类似,但我很难将其他响应适应我的代码。我在使用我的Ios UIScrollView中的触摸交互,ios,objective-c,ipad,uiscrollview,touchesbegan,Ios,Objective C,Ipad,Uiscrollview,Touchesbegan,有一些帖子与此类似,但我很难将其他响应适应我的代码。我在使用我的滚动视图中的触摸开始、触摸移动和触摸结束方法时遇到问题 我目前有一个大的滚动视图(3072 x 2304)。我有一个UIImageView作为这个subview,并且希望检测到其中任何一个的触碰(这不重要,因为它将为我产生相同的结果) 以下是我目前的做法: //Set scrollview to size of image self.myScrollView.contentSize = CGSizeMake(3072, 2304)
滚动视图中的触摸开始
、触摸移动
和触摸结束
方法时遇到问题
我目前有一个大的滚动视图
(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;
}
}
注意:
建议使用手势识别器的方法并不理想。据我所知,除了触摸移动的之外,别无选择;实现这一点的唯一可行方法是对UIScrollView进行子类化,但这不会给我带来任何乐趣。尝试使用此方法。您知道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
}
我不太明白这一点。UITapGestureRecognitor肯定只能检测单触事件吗?我还需要能够跟踪触摸运动。在这种情况下,您需要将该手势也添加到您的滚动视图中。我已经尝试使用此方法。问题在于,UITapGestureRecognitor无法在触地事件后检测到移动-其行为类似于触地开始。我需要一些行为类似于TouchsMoved的东西。根据我的说法,如果滚动视图是子视图,则无法在滚动视图上获取事件。在这种情况下,您需要添加getsture。我的目标是创建处理触摸的UIScrollView的子视图。ToucheSBegind/touchesEnded方法仅在UIView中工作。所以这是我试图纠正的一个错误。