gpt4 book ai didi

objective-c - UIImageView 标签和点击手势问题

转载 作者:行者123 更新时间:2023-11-28 20:44:06 26 4
gpt4 key购买 nike

我正在开发适用于 iPad 的 child 图书应用程序。它有一个加载 UIImageView 以显示 UIImages (JPEG's) 的 UIView,用户可以在图像上滑动以浏览页面——一切正常。现在我想通过添加另一个 UIImageView 来为一些页面添加一些交互性,它会加载一个 PNG 文件,并且在点击手势上我想为它们设置动画......下面是代码片段......

我在 viewDidLoad 中向 UIView 添加了一个点击手势。 viewDidLoad 调用 loadPage 并在 loadPage 内部我以编程方式添加一个包含 PNG 文件的 UIImageView (imageAnimation) 并为其分配一个标签,以便我可以根据 handleTap 例程中的标签播放动画。出于某种原因,handleTap 中的 switch 语句仅针对情况 1 执行,对于其他情况,永远不会调用 handleTap 例程。我做错了什么?

#import "KidsViewController.h"

@implementation KidsViewController
@synthesize imageAnimation;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UISlider class]] || [touch.view isKindOfClass:[UIButton class]])
{
return NO;
}
return YES;
}

- (void)handleTap:(UITapGestureRecognizer *)recognizer {

NSLog(@"KidsViewController ==> handleTap.");

switch (((UIGestureRecognizer *)recognizer).view.tag)
{
case 1:
//...
NSLog(@"KidsViewController ==> handleTap. Switch Case: %d", 1);
break;
case 2:
//...
NSLog(@"KidsViewController ==> handleTap. Switch Case: %d", 2);
break;
case 3:
//...
NSLog(@"KidsViewController ==> handleTap. Switch Case: %d", 3);
break;
default:
NSLog(@"KidsViewController ==> handleTap. Switch Case: DEFAULT");
break;
}

}

- (void)viewDidLoad {

pageCount=12;
pageNum=1;

//put imageviews in place
imageNext.frame=CGRectMake(0,0-crop,screenwidth,screenheight+(crop*2));
imageCurrent.frame=CGRectMake(0,0-crop,screenwidth,screenheight+(crop*2));

[self loadPage];

imageCurrent.image = [UIImage imageWithContentsOfFile:[self filePathForLanguage:language pageNumber:pageNum fileType:@"jpg"]];

//TAP GESTURE
UITapGestureRecognizer *tapRecognizer;
tapRecognizer=[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTap:)];
tapRecognizer.numberOfTapsRequired=1;
tapRecognizer.numberOfTouchesRequired=1;
[self.imageAnimation addGestureRecognizer:tapRecognizer];
tapRecognizer.delegate = self;
[tapRecognizer release];
}

-(void)loadPage{

imageNext.image = [UIImage imageWithContentsOfFile:[self filePathForLanguage:language pageNumber:pageNum fileType:@"jpg"]]; //[UIImage imageWithContentsOfFile:pathFilename];

switch (pageNum)
{
case 1:
//...
NSLog(@"KidsViewController ==> loadPage. Switch Case: %d", pageNum);
UIImage *image = [UIImage imageNamed:@"P3-stilts_00000.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
imageAnimation = [[UIImageView alloc] initWithFrame:frame];
imageAnimation.userInteractionEnabled = YES;
imageAnimation.image = image;
imageAnimation.tag = pageNum;
[self.view addSubview:imageAnimation];
[image release];
break;
case 2:
//...
NSLog(@"KidsViewController ==> loadPage. Switch Case: %d", pageNum);
imageAnimation.image = nil;
[imageAnimation setCenter:CGPointMake(-100,-100)];
break;
case 3:
//...
NSLog(@"KidsViewController ==> loadPage. Switch Case: %d", pageNum);
UIImage *image3 = [UIImage imageNamed:@"bug.png"];
CGRect bugFrame = CGRectMake(0, 0, image3.size.width, image3.size.height);
imageAnimation = [[UIImageView alloc] initWithFrame:bugFrame];
imageAnimation.userInteractionEnabled = YES;
imageAnimation.image = image3;
imageAnimation.tag = pageNum;
[self.view addSubview:imageAnimation];
[image3 release];
break;
default:
NSLog(@"KidsViewController ==> loadPage. Switch Case: DEFAULT");
[imageAnimation setCenter:CGPointMake(-100,-100)];
break;
}
}

- (void)dealloc {
[setupViewController release];
[imageCurrent release];
[imageNext release];
[imageShadow release];
[imageMenuBar release];
[imageAnimation release];
[super dealloc];
}

@end

最佳答案

你总能得到你的self.view标签。它的标签默认为0。所以 switch 跳转到默认选项。

您可以将您的识别器添加到 imageAnimation,它会正常工作。

关于objective-c - UIImageView 标签和点击手势问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7208274/

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