我想要实现的是创建一种功能,当用户点击图像时,它会推送到navController中的下一个VC。
ViewDidLoad中的:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[self.scroll addGestureRecognizer:singleTap];
在singleTapAction中:
- (void)singleTapAction:(UIGestureRecognizer *)singleTap {
UIView *view = singleTap.view;
if([view isKindOfClass:[UIImageView class]]){
NSLog(@"tapped on image");
}
else{
NSLog(@"Just tapped");
}
}
if-logic似乎不起作用。它始终在日志中提供“Just tapped”。即使我点击UIImageView,它也会检测到我点击了scrollview,这是ImageView的容器。
(lldb) po view;
$0 = 0x1f5ed6e0 <UIScrollView: 0x1f5ed6e0; frame = (0 0; 320 416); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1f5edc20>; layer = <CALayer: 0x1f5ecb80>; contentOffset: {0, 0}>
编辑:
我使用imageview填充scrollview的方式是:
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (nil != group) {
// be sure to filter the group so you only get photos
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
NSLog(@"%d images found", group.numberOfAssets);
// for(int i = group.numberOfAssets - 5; i<group.numberOfAssets - 1; i++){
dispatch_apply(4, dispatch_get_global_queue(0, 0), ^(size_t i){
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i]
options:0
usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (nil != result) {
ALAssetRepresentation *repr = [result defaultRepresentation];
// this is the most recent saved photo
UIImage *img = [self fixrotation:[UIImage imageWithCGImage:[repr fullResolutionImage]]];
UIImageOrientation orient = img.imageOrientation;
NSLog(@"orientation: %d", orient);
CGFloat aspectRatio = img.size.width/img.size.height;
UIImageView *imgView = [[UIImageView alloc] init];
imgView.frame = CGRectMake(10, self.yCord, 300, 300 /aspectRatio);
self.yCord += margin + (300/aspectRatio);
imgView.image = img;
imgView.layer.shadowRadius = 5.0;
imgView.layer.shadowColor = [UIColor blackColor].CGColor;
imgView.layer.cornerRadius = 4.0;
[imgView setUserInteractionEnabled:YES];
[self.scroll addSubview:imgView];
NSLog(@"%@", imgView);
NSLog(@"%f, %f, %f, %f", imgView.frame.origin.x, imgView.frame.origin.y, imgView.frame.size.width, imgView.frame.size.height);
*stop = YES;
self.scrollViewHeight = self.yCord + imgView.frame.size.height + margin;
CGSize scrollViewSize = CGSizeMake(320, self.scrollViewHeight);
[self.scroll setContentSize:scrollViewSize];
//self.scroll.frame = CGRectMake(0,0, 320, self.scrollViewHeight);
}
}];
});
}
*stop = NO;
} failureBlock:^(NSError *error) {
NSLog(@"error: %@", error);
}];
现在出现的另一个问题是,setUserInteraction
是否在块内工作?
答案 0 :(得分:4)
为什么不在ImageView上添加点击手势而不是
添加滚动...
并制作代码
[self.imageView setUserInteractionEnabled:YES];
答案 1 :(得分:2)
您可以检测触摸是否在图像视图中,如此...
- (void)singleTapAction:(UIGestureRecognizer *)singleTap {
CGPoint touchPoint = [singleTap locationInView:self.scroll];
if(CGRectContainsPoint(self.tappableImageView.frame, touchPoint){
NSLog(@"tapped on image");
}
else{
NSLog(@"Just tapped");
}
}
如果你有多个图像视图,那么做这样的事情......
- (void)singleTapAction:(UIGestureRecognizer *)singleTap {
CGPoint touchPoint = [singleTap locationInView:self.scroll];
UIImageView *theTappedView;
for (UIView *subView in [self.scroll subViews]) {
if(CGRectContainsPoint(self.tappableImageView.frame, touchPoint)
&& [subView isKindOfClass:[UIImageView class]]) {
theTappedView = subView;
break;
}
}
if(theTappedView != nil){
NSLog(@"tapped on image");
}
else{
NSLog(@"Just tapped");
}
}
注意强>
这假设将被点击的所有图像视图都是self.scroll
scrollView的子视图。
答案 2 :(得分:1)
在您的代码中singleTap.view
始终是点击手势附加的视图,而不是您认为点按的视图。
您应该为每个图像视图单独创建和附加点按手势,而不是在容器视图中添加一个。
答案 3 :(得分:-1)
为什么不使用UIButton?
您可以为按钮设置自定义图像。
这样,该按钮可以为您处理点击事件,而不是您编写自己的点击手势处理程序。
我注意到你的代码中有:
UIView * view = gesture.view;
您是否尝试将其更改为:
UIImageView * view = gesture.view;
顺便说一句,当您将图像添加到滚动视图时,您可能希望立即将该手势识别器添加到该图像,而不是将其添加到滚动视图中。