ios跑马灯

来源:互联网 发布:数据库迁移 编辑:程序博客网 时间:2024/04/29 15:57

之前项目做一个类似跑马灯的效果

因为项目需求每次回到跑马灯页面都要从第一个开始动画

所以刚开始就用了UIView的Animations动画,在动画结束后调用一个方法来改变数据然后继续Animations,在没离开这个页面的时候都是正常的,可是没想到离开这个页面后 这个动画的时间好像就不起作用了似的,一直在互相掉没有延迟时间了!一直没找到问题的所在;


之后在开发过程中突然想到用系统的延迟方法:

[self performSelector:@selector(startTimer) withObject:nil afterDelay:kStartTime];

替换Animations的

+ (void)setAnimationDidStopSelector:(SEL)selector; 
方法来做动画结束的功能


代码例子:

-(void)startTimer{
    
    
    textIndex++;
    [textLabel setViewX:300];
    if (textIndex >= [textArray count])
        textIndex = 0;
    
    textLabel.text =[textArray objectAtIndex:textIndex];
   
    if (isAnimation) {
        
        [self textRunner];
        
    }
    
}

-(void)textRunner{
    // -------滚动
    [UIView beginAnimations:@"testAnimation" context:NULL];
    [UIView setAnimationDuration:kStartTime];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatAutoreverses:NO];     //不重复播放
    
    //使用延迟方法替代动画结束调用方法功能
    [self performSelector:@selector(startTimer) withObject:nil afterDelay:kStartTime];
    

    [textLabel setViewX:-400];
    
    
    [UIView commitAnimations];
    
}

如果有需求的话可以调用

 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startTimer) object:nil];

来停止动画的互相调用




如果有朋友知道我的问题出在哪里指点一下小弟

为什么离开页面后他的延迟时间就不好使了!!!!!!!!!!!!!!!!!!!!

原创粉丝点击