我有一个有定时器的应用程序。它从10到0.使用NSTimer。我说过: 的.m:
//
// Skin.m
// PopThatPimple
//
// Created by Rohan Kapur on 6/2/11.
// Copyright 2011 UWCSEA. All rights reserved.
//
#import "Skin.h"
@implementation Skin
@synthesize theAudio;
@synthesize label;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint point = [myTouch locationInView:pimple];
if ( CGRectContainsPoint(pimple.bounds, point) ) {
[self checkcollision];
}
}
-(void)checkcollision {
pimple.center = CGPointMake(
random() % (unsigned int)theview.bounds.size.width,
random() % (unsigned int)theview.bounds.size.height
);
score.text = [NSString stringWithFormat:@"%d", ([score.text intValue] + 1)];
sad.hidden = NO;
NSURL* popURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"pop" ofType:@"mp3"]];
AudioServicesCreateSystemSoundID((CFURLRef) popURL, &popID);
AudioServicesPlaySystemSound(popID);
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
-(void)showActivity{
int currentTime = [time.text intValue];
int newTime = currentTime + -1;
time.text = [NSString stringWithFormat:@"%d", newTime];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)viewDidLoad {
myTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector (showActivity) userInfo:nil repeats:YES];
[label2 setHidden:YES];
[super viewDidLoad];
}
-(void)void2 {
if ([label3.text isEqualToString:@"0"]) {
[[self view] addSubview:timeup];
score.text = label2.text;
label2.hidden = NO;
}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
-(void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
-(void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
·H:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface Skin : UIViewController {
IBOutlet UIImageView *pimple;
IBOutlet UIImageView *smile;
IBOutlet UIImageView *sad;
AVAudioPlayer *theAudio;
IBOutlet UIView *theview;
SystemSoundID popID;
IBOutlet UIImageView *face;
IBOutlet UIImageView *face2;
IBOutlet UIImageView *face3;
IBOutlet UIImageView *face4;
IBOutlet UIImageView *face5;
IBOutlet UIImageView *face6;
IBOutlet UIImageView *face7;
IBOutlet UIImageView *face8;
IBOutlet UIImageView *face9;
IBOutlet UIImageView *face10;
IBOutlet UIImageView *face11;
IBOutlet UIImageView *face12;
IBOutlet UIImageView *face13;
IBOutlet UIImageView *face14;
IBOutlet UIImageView *face15;
IBOutlet UILabel *label2;
IBOutlet UILabel *score;
IBOutlet UIImageView *skin;
IBOutlet UILabel *time;
NSTimer * myTicker;
IBOutlet UILabel *label3;
IBOutlet UIImageView * timeup;
}
-(IBAction)start;
-(void)void2;
-(void)showActivity;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain)AVAudioPlayer *theAudio;
-(void)checkcollision;
@end
我还没有使用过我所有的网点和习惯。我还没有使用过avfoundation 我已经连接了一切!我已经做了很多次!当它达到零时,没有任何事情发生,计时器只会进入底片。我究竟做错了什么?
答案 0 :(得分:2)
这里有两个主要问题,第一个是使用赋值运算符,而不是比较运算符,我想你会发出警告。
第二个是==
比较在这里没用,固定代码就是。
if ([label3.text isEqualToString:@"0"])
答案 1 :(得分:1)
我的第二个(第三个?)是@BoltClock和@Joshua Weinberg写的,但是补充一点,检查标签中显示的文字是判断计时器是否已过期的一种不好的方法。在某个时刻,在程序的某个地方,您将将标签的文本设置为@“0”,因此您已经知道该条件为真。同样的分数...你的控制器应知道得分是什么,并且绝对不应该依赖标签来获得它。
不要在视图对象中存储数据。数据应该从模型通过控制器流向视图。当您接受用户输入时,它只会向相反方向流动。