兩種狀態的改變通過動畫來漸變,設置動畫效果,一般采用的方式有以下幾種:
方式一:
[UIView beginAnimations:(NSString *) context:<#(void *)#>];[UIView setAnimationDuration:<#(NSTimeInterval)#>];/*****這里插入需要產生動畫的片段*****/ [UIViewcommitAnimations];
其中NSString和void這兩個參數可設為nil;NSTimeInterval是設置播放持續的時間,單位秒;
方式二:
[UIView animateWithDuration:<#(NSTimeInterval)#> animations:^{ /*****這里插入需要產生動畫的片段*****/ }];
NSTimeInterval依然是設置播放持續的時間,單位秒。
方式三:
UIView animateWithDuration:<#(NSTimeInterval)#> animations:^{ /*****這里插入需要產生動畫的片段*****/ } completion:^(BOOL finished) { /*****這里插入播放完動畫后要做的事情*****/ }
NSTimeInterval依然是設置播放持續的時間,單位秒;animations
另外UIImageView也有播放幀動畫的功能:
- (IBAction)btnClick:(UIButton *)sender { //取出按鈕的文本名 NSString *title = [sender titleForState:UIControlStateNormal]; //根據文本名在字典中取得對應的圖片數量個數 int picsCount = [_dict[title] intValue]; NSMutableArray *imgArray = [NSMutableArray array] ; //創建圖片 //生成文件名列表 for(int i = 0; i < picsCount; i++){ //生成文件名 拼接全路徑 NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg", title, i]; NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil]; //圖片初始化及添加到數組中去 UIImage *img = [[UIImage alloc] initWithContentsOfFile:path]; [imgArray addObject:img]; } if(!_imageView.isAnimating){ //判斷是否正在播放動畫,防止被打斷 _imageView.animationImages = imgArray; _imageView.animationDuration = 0.1 * picsCount; //設置動畫持續時間,單位秒 _imageView.animationRepeatCount = 1; //設置播放次數 [_imageView startAnimating]; }}
新聞熱點
疑難解答