【问题标题】:How to stop a tap from counting when the timer is paused in xcode当定时器在xcode中暂停时如何停止点击计数
【发布时间】:2011-09-06 21:49:51
【问题描述】:

我正在设计一个 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];
}

【问题讨论】:

    标签: iphone ios xcode uitouch


    【解决方案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
    }
    

    【讨论】:

    • 这是您想要的方法。但是,我建议使用状态变量 (BOOL shouldCountPoints) 来检查是否应该添加点 - 可能会出现按钮未隐藏但出于某种原因想要计算点的情况。跨度>
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 2016-10-15
      相关资源
      最近更新 更多