嗨,我有一个问题,我几天都无法想象。我有一个touchesBegan方法,我想用它来判断有人正在点击UIImageView,以便它可以使用NSTimers上下移动不同的UIImageView。由于某种原因,我的TouchesBegan不会工作,我知道这是问题,因为我测试了NSLogs,结果指向没有被调用的方法。这是我的代码,谢谢 附:如果有一个超级快速修复,它让我看起来很蠢,我很抱歉,我现在只开发了6天。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self.view];
if (CGRectContainsPoint(flyImageButton.frame, pt)) {
flying = YES;
[playerFall invalidate];
playerFlyUp = [NSTimer scheduledTimerWithTimeInterval:.03 target:self selector:@selector(flyUp) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:playerFlyUp forMode:NSRunLoopCommonModes];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (flying == YES) {
flying = NO;
[playerFlyUp invalidate];
[self startFallTimer];
}
}
这是我的.h文件,供那个问...
的人#import <UIKit/UIKit.h>
@interface GameViewController : UIViewController {
IBOutlet UILabel *scoreLabel, *coinsLabel;
IBOutlet UIImageView *flyImageButton, *playerImage, *flyingObject;
IBOutlet UIScrollView *scroll;
int score;
int coins;
BOOL flying;
NSTimer *playerFlyUp, *playerFall, *scoreTimer, *coinsTimer;
}
@end
我也尝试过UITouch ......
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//CGPoint pt = [[touches anyObject] locationInView:self.view];
UITouch *touch = [[event allTouches]anyObject];
//if (CGRectContainsPoint(flyImageButton.frame, pt)) {
if ([touch view] == flyImageButton) {
flying = YES;
[playerFall invalidate];
playerFlyUp = [NSTimer scheduledTimerWithTimeInterval:.03 target:self selector:@selector(flyUp) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:playerFlyUp forMode:NSRunLoopCommonModes];
}
}
让我感到困惑的是,无论我触摸到哪里,NSLog“Pos 1”都不会触发......
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//CGPoint pt = [[touches anyObject] locationInView:self.view];
UITouch *touch = [[event allTouches]anyObject];
CGPoint loc = [touch locationInView:self.view];
//This NSLog never displays leading me to think that my CGPoint isn't the issue...
NSLog(@"Pos 1");
if (CGRectContainsPoint(flyImageButton.frame, loc)) {
//if ([touch view] == flyImageButton) {
NSLog(@"Pos 2");
flying = YES;
[playerFall invalidate];
playerFlyUp = [NSTimer scheduledTimerWithTimeInterval:.03 target:self selector:@selector(flyUp) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:playerFlyUp forMode:NSRunLoopCommonModes];
}
}
答案 0 :(得分:0)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [[event allTouches]anyObject];
}
答案 1 :(得分:0)
CGPoint pt = [[touches anyObject] locationInView:nameofyour_imageview];
将此更改为指向您的UIImageview而非self.view或尝试点按手势识别器