淡入淡出UIView動畫

因爲最近在忙個項目,所以有一段時間沒更新博客了


好,直奔主題:

先在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;
        }];
    }
}

最後,大功告成


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章