gpt4 book ai didi

ios - 检测用户是否点击了UIImageView

转载 作者:行者123 更新时间:2023-12-01 17:41:50 25 4
gpt4 key购买 nike

我要实现的目的是创建一个功能,当用户单击图像时,该功能将推送到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似乎不起作用。它总是在日志中提供“仅点击”。即使单击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}>

编辑:

我用imageviews填充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是否在块内工作?

最佳答案

为什么不在ImageView上添加轻击手势而不是

添加滚动...

并编写代码

[self.imageView setUserInteractionEnabled:YES];

关于ios - 检测用户是否点击了UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16734544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com