我正在制作适用于iPad的Kid's Book App。它有一个UIView加载UIImageView来显示UIImages(JPEG),用户可以在图像上滑动以浏览页面 - 一切正常。现在我想通过添加另一个加载PNG文件的UIImageView来添加一些页面的交互性,而在Tap Gesture上我想要为它们设置动画...下面是代码片段......
我在viewDidLoad中为UIView添加了一个Tap Gesture。 viewDidLoad调用loadPage和loadPage内部我以编程方式添加包含PNG文件的UIImageView(imageAnimation),并为其分配标签,以便我可以根据handleTap例程中的标签播放动画。出于某种原因,handleTap中的switch语句仅针对案例1执行ON,对于其他情况,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
答案 0 :(得分:2)
您始终会获得 self.view 的标记。默认情况下,标记为 0 。因此切换跳转到默认选项。
您可以将识别器添加到 imageAnimation ,它会正常工作。
答案 1 :(得分:1)
你的问题可能就在这里:
switch (((UIGestureRecognizer *)recognizer).view.tag)
当您注册该手势识别器时,您将添加到视图控制器的视图中,而不是您正在追踪的图像视图:
[self.view addGestureRecognizer:tapRecognizer];
如果您尝试获取被点击的视图,请考虑使用类似
的内容CGPoint point = [tapRecognizer locationInView:tapRecognizer.view];
UIView *viewThatWasTouched = [tapRecognizer.view hitTest:point withEvent:nil];