如何在xcode中暂停计时器时停止计数

时间:2011-09-06 21:49:52

标签: iphone ios xcode uitouch

我正在设计一款iphone应用程序,基本上让用户点击屏幕上的球,应用程序计算用户点击球的次数,问题是,应用程序计算点击开始按钮之前的点击。在点击开始按钮之前,我如何阻止点击计数到你的分数?

这是我的代码:

·H:

@interface FlipsideViewController : UIViewController {
    IBOutlet UIImageView *Ball1;
    NSTimer *mainTimer1;
    IBOutlet UIButton *startButton1;
    IBOutlet UILabel *currentNumber1;
    IBOutlet UIButton *pause1;
    UIAlertView *alertStart1;
}

@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
@property (nonatomic, retain)  UIImageView *Ball1;
@property (nonatomic, retain)  NSTimer *mainTimer1;
@property (nonatomic, retain)  UIButton *startButton1;

- (IBAction)done:(id)sender;
- (IBAction)startMove;
- (void)moveBall;
- (BOOL)Intersecting:(CGPoint)loctouch:(UIImageView *)enemyimg;
- (IBAction)pause1:(id)sender;
- (void)alertStart1;

@end

的.m:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view]; 
// if user touched ball, stop timer 
if ([self Intersecting:location :Ball1]) {
    [mainTimer1 invalidate];
    mainTimer1 = nil;
    startButton1.hidden = NO;
    number++;
    [currentNumber1 setText:[NSString stringWithFormat:@"%d", number]];
    }
}   

-(BOOL)Intersecting:(CGPoint)loctouch:(UIImageView *)enemyimg {
CGFloat x1 = loctouch.x;
CGFloat y1 = loctouch.y;

CGFloat x2 = enemyimg.frame.origin.x;
CGFloat y2 = enemyimg.frame.origin.y;
CGFloat w2 = enemyimg.frame.size.width;
CGFloat h2 = enemyimg.frame.size.height;

if ((x1>x2)&&(x1<x2+w2)&&(y1>y2)&&(y1<y2+h2))
    return YES;
else
    return NO;

}

-(void)moveBall {
CGFloat xdist = fabs(Destination.x-Ball1.center.x);
CGFloat ydist = fabs(Destination.y-Ball1.center.y);

if ((xdist<5)&&(ydist<5)) { 
    Destination = CGPointMake(arc4random() % 320, arc4random() % 480);
    xamt = ((Destination.x - Ball1.center.x) / speed);
    yamt = ((Destination.y - Ball1.center.y) / speed);
} else {
    Ball1.center = CGPointMake(Ball1.center.x+xamt, Ball1.center.y+yamt);
}
}

-(IBAction)startMove {
startButton1.hidden = YES;

Destination = CGPointMake(arc4random() % 320, arc4random() % 480); 
xamt = ((Destination.x - Ball1.center.x) / speed);
yamt = ((Destination.y - Ball1.center.y) / speed);

mainTimer1 = [NSTimer scheduledTimerWithTimeInterval:(0.02) target:self selector:@selector(moveBall) userInfo:nil repeats: YES];
}

2 个答案:

答案 0 :(得分:1)

方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

检查游戏是否已经开始,例如

if (startButton1.hidden) {
    // game has begun, add points
} else {
    // game hasn't yet begun, don't add points
}

答案 1 :(得分:0)

如果您的应用程序适用于iOS 3.2+,请使用UITapGestureRecognizer代替touchesBegan:withEvent:,然后当用户按下开始按钮时,将识别器添加到imageView。还记得将userInteractionEnabled设置为UIImageView的YES,以便在它们上使用UIGestureRecognizer。