我想要实现的是创建一个功能,当用户单击图像时,它会推送到 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 逻辑似乎不起作用。它总是在日志中给出“刚刚点击”。即使当我点击 UIImageView 时,它也会检测到我点击了滚动视图,这是 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}>
编辑:
我用 imageviews 填充滚动视图的方式是:
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
在块内有效吗?