NSTimer 在 iPhone 动画中工作不正常

发布于 2024-09-09 04:34:12 字数 5179 浏览 2 评论 0原文

我是 iPhone 编程新手。在我的应用程序中,计时器工作不正常。 有人可以帮我吗... 以下是我们触摸或滑动时对一些图像进行动画处理的代码..它们掉落下来

我想使用计时器将触摸限制几秒钟... 有另一种方法可以做到这一点..?(在使用计时器之前,它在设备上崩溃,但在模拟器中工作正常..我正在尝试解决崩溃问题..)

//  HomeViewController.m


#import "HomeViewController.h"

@implementation HomeViewController

@synthesize dustImage, myAnimatedView;

@synthesize _isAnimating = isAnimating;

float ver_X, ver_Y;
BOOL rotated;
NSTimer *touchTimer;


- (void)viewDidLoad {

    [super viewDidLoad];
    self.view.frame  = [[UIScreen mainScreen] bounds];
    //[self playAudio];

}

-(void)blowingTheSparkles:(NSSet *) touches {

    if(!isAnimating){
        float a, aa; 
        float b, bb;

        CGRect frame1;
        myAnimatedView = [UIImageView alloc];

        //NSLog(@"touch count...%d",[touches count]);
        for (int i=0; i< (int)[touches count]; ++i) {
            UITouch* touch = [[touches allObjects] objectAtIndex:i];
            CGPoint location = [touch locationInView:dustImage];

            NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"starburst1.png"],
                                 [UIImage imageNamed:@"starburst2.png"], 
                                 [UIImage imageNamed:@"starburst3.png"], 
                                 [UIImage imageNamed:@"starburst4.gif"],
                                 [UIImage imageNamed:@"starburst5.png"],
                                 [UIImage imageNamed:@"starburst6.png"],
                                 [UIImage imageNamed:@"starburst7.png"],nil]; 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
            a=(location.x)-80;
            b=(location.y)-80;
            ver_X=768;
            ver_Y=1024;     

            frame1 = CGRectMake(a,b,160,160);

#else

            a=(location.x)-40;
            b=(location.y)-40;
            ver_X=320;
            ver_Y=480;

            frame1 = CGRectMake(a,b,80,80);

#endif
            aa= location.x;
            bb= location.y;
            [myAnimatedView initWithFrame:frame1]; 
            myAnimatedView.animationImages = myImages; 
            myAnimatedView.animationDuration = 0.25;
            myAnimatedView.animationRepeatCount = 0;

            [dustImage addSubview:myAnimatedView];


            CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
            [fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]];
            fadeOutAnimation.fillMode = kCAFillModeForwards;
            fadeOutAnimation.removedOnCompletion = NO;

            //Set up scaling
            CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
            [resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(0.0f, frame1.size.height * (1.0f /1.0))]];
            resizeAnimation.fillMode = kCAFillModeForwards;
            resizeAnimation.removedOnCompletion = NO;

            // Set up path movement
            CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            pathAnimation.calculationMode = kCAAnimationPaced;
            pathAnimation.fillMode = kCAFillModeForwards;
            pathAnimation.removedOnCompletion = NO;

            CGPoint endPoint = CGPointMake(aa ,ver_Y);
            CGMutablePathRef curvedPath = CGPathCreateMutable();
            CGPathMoveToPoint(curvedPath, NULL, aa, bb);
            CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, bb,endPoint.x, bb,endPoint.x,endPoint.y);
            pathAnimation.path = curvedPath;
            CGPathRelease(curvedPath);

            CAAnimationGroup *group = [CAAnimationGroup animation]; 
            group.fillMode = kCAFillModeForwards;
            group.removedOnCompletion = NO;
            [group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]];
            group.duration = 4.0f;
            group.delegate = self;
            [group setValue:myAnimatedView forKey:@"myAnimatedView"];

            [myAnimatedView.layer addAnimation:group forKey:@"savingAnimation"];
            [myAnimatedView startAnimating];


        }   
    }
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(!isAnimating)
        [self blowingTheSparkles:touches];

}

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


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

    isAnimating=TRUE;
    touchTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sleeping) userInfo:nil repeats:NO];

}

-(void)sleeping{

    NSLog(@"....Hiiii");
    isAnimating = FALSE;
    [touchTimer invalidate];
    touchTimer = nil;

}

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

    if(!isAnimating){
        [self blowingTheSparkles:touches];

        }

}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return NO;
}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];   

}

- (void)viewDidUnload {

    [super viewDidUnload];

}


- (void)dealloc {

    [myAnimatedView release];

    [super dealloc];

}

@end

im new to iPhone programming..in my app the timer is not working fine.
can anybody help me out please...
The following is the code for animating few images wn we touch or swipe..they fall down

i want to restrict the touches for few seconds with use of timer...
is there another way to do this..?(before using timer its crashing on device,but working fine in the simulator..im trying for solution for crashing..)

//  HomeViewController.m


#import "HomeViewController.h"

@implementation HomeViewController

@synthesize dustImage, myAnimatedView;

@synthesize _isAnimating = isAnimating;

float ver_X, ver_Y;
BOOL rotated;
NSTimer *touchTimer;


- (void)viewDidLoad {

    [super viewDidLoad];
    self.view.frame  = [[UIScreen mainScreen] bounds];
    //[self playAudio];

}

-(void)blowingTheSparkles:(NSSet *) touches {

    if(!isAnimating){
        float a, aa; 
        float b, bb;

        CGRect frame1;
        myAnimatedView = [UIImageView alloc];

        //NSLog(@"touch count...%d",[touches count]);
        for (int i=0; i< (int)[touches count]; ++i) {
            UITouch* touch = [[touches allObjects] objectAtIndex:i];
            CGPoint location = [touch locationInView:dustImage];

            NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"starburst1.png"],
                                 [UIImage imageNamed:@"starburst2.png"], 
                                 [UIImage imageNamed:@"starburst3.png"], 
                                 [UIImage imageNamed:@"starburst4.gif"],
                                 [UIImage imageNamed:@"starburst5.png"],
                                 [UIImage imageNamed:@"starburst6.png"],
                                 [UIImage imageNamed:@"starburst7.png"],nil]; 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
            a=(location.x)-80;
            b=(location.y)-80;
            ver_X=768;
            ver_Y=1024;     

            frame1 = CGRectMake(a,b,160,160);

#else

            a=(location.x)-40;
            b=(location.y)-40;
            ver_X=320;
            ver_Y=480;

            frame1 = CGRectMake(a,b,80,80);

#endif
            aa= location.x;
            bb= location.y;
            [myAnimatedView initWithFrame:frame1]; 
            myAnimatedView.animationImages = myImages; 
            myAnimatedView.animationDuration = 0.25;
            myAnimatedView.animationRepeatCount = 0;

            [dustImage addSubview:myAnimatedView];


            CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
            [fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]];
            fadeOutAnimation.fillMode = kCAFillModeForwards;
            fadeOutAnimation.removedOnCompletion = NO;

            //Set up scaling
            CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
            [resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(0.0f, frame1.size.height * (1.0f /1.0))]];
            resizeAnimation.fillMode = kCAFillModeForwards;
            resizeAnimation.removedOnCompletion = NO;

            // Set up path movement
            CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            pathAnimation.calculationMode = kCAAnimationPaced;
            pathAnimation.fillMode = kCAFillModeForwards;
            pathAnimation.removedOnCompletion = NO;

            CGPoint endPoint = CGPointMake(aa ,ver_Y);
            CGMutablePathRef curvedPath = CGPathCreateMutable();
            CGPathMoveToPoint(curvedPath, NULL, aa, bb);
            CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, bb,endPoint.x, bb,endPoint.x,endPoint.y);
            pathAnimation.path = curvedPath;
            CGPathRelease(curvedPath);

            CAAnimationGroup *group = [CAAnimationGroup animation]; 
            group.fillMode = kCAFillModeForwards;
            group.removedOnCompletion = NO;
            [group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]];
            group.duration = 4.0f;
            group.delegate = self;
            [group setValue:myAnimatedView forKey:@"myAnimatedView"];

            [myAnimatedView.layer addAnimation:group forKey:@"savingAnimation"];
            [myAnimatedView startAnimating];


        }   
    }
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(!isAnimating)
        [self blowingTheSparkles:touches];

}

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


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

    isAnimating=TRUE;
    touchTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sleeping) userInfo:nil repeats:NO];

}

-(void)sleeping{

    NSLog(@"....Hiiii");
    isAnimating = FALSE;
    [touchTimer invalidate];
    touchTimer = nil;

}

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

    if(!isAnimating){
        [self blowingTheSparkles:touches];

        }

}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return NO;
}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];   

}

- (void)viewDidUnload {

    [super viewDidUnload];

}


- (void)dealloc {

    [myAnimatedView release];

    [super dealloc];

}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你的星星会说话 2024-09-16 04:34:12

您似乎没有正确保留 NSTimer,这可能是您的问题。

将其更改为:

touchTimer = [[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sleeping) userInfo:nil repeats:NO] retain];

理想情况下,您应该将 touchTimer 声明为属性并合成它,但请先尝试此操作。

更改睡眠以释放计时器:

-(void)sleeping{

    NSLog(@"....Hiiii");
    isAnimating = FALSE;
    [touchTimer invalidate];
    [touchTimer release];
    touchTimer = nil;
}

You don't seem to be correctly retaining the NSTimer which could be your problem here.

Change it to:

touchTimer = [[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sleeping) userInfo:nil repeats:NO] retain];

Ideally you should declare touchTimer as a property and synthesize it, but try this first.

Change sleeping to release the timer:

-(void)sleeping{

    NSLog(@"....Hiiii");
    isAnimating = FALSE;
    [touchTimer invalidate];
    [touchTimer release];
    touchTimer = nil;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文