淡入淡出UIView動畫
阿新 • • 發佈:2019-02-05
因為最近在忙個專案,所以有一段時間沒更新部落格了
好,直奔主題:
先在viewDidLoad裡新增一個將要執行動畫的UIView,下面的程式碼注意要先把這個UIView隱藏
//產生一個提示框 _remindLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 385, WIDTH_OF_LABEL, HEIGHT_OF_LABEL)]; self.remindLabel.backgroundColor = [UIColor clearColor]; [self.remindLabel setTextColor:[UIColor whiteColor]]; [self.remindLabel setFont:[UIFont systemFontOfSize:13.0]]; [self.view addSubview:self.remindLabel]; [self.view bringSubviewToFront:self.remindLabel]; self.remindLabel.hidden = YES; [_remindLabel release];
在點選某個按鈕的時候執行下面方法:
- (void)downloadBtnFun { DNData *data = [DNData shareInstance]; int index = fabs(self.scrollView.contentOffset.x / self.view.bounds.size.width); ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; UIImage *image = [UIImage imageNamed:[data.imageArray objectAtIndex:index]]; [library writeImageToSavedPhotosAlbum:[image CGImage]orientation:ALAssetOrientationUp completionBlock:^(NSURL *url, NSError *error) { if (error) { self.remindLabel.text = @"圖片儲存失敗"; } else { self.remindLabel.text = @"圖片已存入相簿"; } }]; if (self.remindLabel.hidden == YES) { self.remindLabel.hidden = NO; self.remindLabel.alpha = 0.3; [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^ { self.remindLabel.alpha = 1.0; NSLog(@"in animate start"); } completion:^(BOOL finished) { NSLog(@"in animate completion"); }]; } self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(disappearLabel) userInfo:nil repeats:NO]; }
在timer的方法中執行消失動畫:
- (void)disappearLabel { if (self.remindLabel.hidden == NO) { self.remindLabel.alpha = 1.0; [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^ { self.remindLabel.alpha = 0.0; NSLog(@"out animate start"); }completion:^(BOOL finished) { NSLog(@"out animate completion"); if (self.timer) { [self.timer invalidate]; self.timer = nil; } self.remindLabel.hidden = YES; }]; } }
最後,大功告成