我使用的是两个NSTimer,用于生成图像。另一个我用来暂停图像几秒钟。 我已经成功暂停了图像。 10秒后新图像也开始出现,但我面临的问题是我希望静止图像与新图像一起开始移动。这是我用来暂停图像并移动它的代码。
- (void)viewDidLoad
{
timerCount=0;
}
- (IBAction)didPauseBalloons:(id)sender
{
[self countdownTimer];
}
-(void)updateCounter : (NSTimer *) theTimer
{
timerCount++;
if (timerCount > 10) {
[theTimer invalidate];
}
if(secondLeft > 0 ){
secondLeft -- ;
hoursLeft = secondLeft / 3600;
minutesLeft = (secondLeft % 3600) / 60;
secondsLeft = (secondLeft %3600) % 60;
}
else{
secondLeft = 10;
}
}
-(void)countdownTimer
{
secondLeft = hoursLeft = minutesLeft = secondsLeft = 0;
if([timeResume isValid])
{
}
timeResume = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
[timer setFireDate:[NSDate distantFuture]];
[countDown setFireDate:[NSDate distantFuture]];
for (int i=0; i<balloons.count; i++) {
Balloon *ballon = [balloons objectAtIndex:i];
CFTimeInterval pausedTime = [ballon.layer convertTime:CACurrentMediaTime() fromLayer:nil];
ballon.layer.speed = 0.0;
ballon.layer.timeOffset = pausedTime;
[self resumeBalloons];
}
}
-(void)resumeBalloons
{
[timer setFireDate:[NSDate date]];
[countDown setFireDate:[NSDate date]];
for (int i=0; i<balloons.count; i++) {
Balloon *ballon = [balloons objectAtIndex:i];
CFTimeInterval pausedTime = [ballon.layer timeOffset];
ballon.layer.speed = 1.0;
ballon.layer.timeOffset = 0.0;
ballon.layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [ballon.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
ballon.layer.beginTime = timeSincePause;
}
.m的气球类
#import "Balloon.h"
@implementation Balloon
@synthesize baloon;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
baloon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[baloon setUserInteractionEnabled:YES];
[baloon setExclusiveTouch:YES];
[self addSubview:baloon];
}
return self;
}