因为最近在忙个项目,所以有一段时间没更新博客了
好,直奔主题:
先在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;
}];
}
}
最后,大功告成