#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()<CAAnimationDelegate>{
}
@property (nonatomic , strong) UIView *backView;
@property (nonatomic , strong) UIImageView *imageV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *taskBtn = [UIButton buttonWithType:UIButtonTypeCustom];
taskBtn.frame = CGRectMake(kScreenWidth-100, kScreenHeight-150, 65, 67);
[taskBtn setBackgroundImage:[UIImage imageNamed:@"task_button_image"] forState:UIControlStateNormal];
[taskBtn addTarget:self action:@selector(taskButtonClickEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:taskBtn];
}
#pragma mark --- backView create
-(void)setUpBackView{
if (!_backView) {
_backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
_backView.backgroundColor = [UIColorFromRGB(0x000000) colorWithAlphaComponent:0.5];
[self.view addSubview:_backView];
_backView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backViewClickEvent)];
[_backView addGestureRecognizer:singleTap];
_imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
_imageV.image = [UIImage imageNamed:@"task_view_image"];
[_backView addSubview:_imageV];
// [_imageV mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.mas_equalTo(0);
// make.top.mas_equalTo(0);
// make.width.mas_equalTo(kScreenWidth);
// make.height.mas_equalTo(kScreenHeight);
//
// }];
//
// [self.imageV layoutIfNeeded];
}
}
#pragma mark --- 点击事件
-(void)taskButtonClickEvent{
[self setUpBackView];
CABasicAnimation *animationScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animationScale.duration = 0.2;
animationScale.repeatCount = 1;
animationScale.fromValue = @0.0;
animationScale.toValue = @1.0;
animationScale.removedOnCompletion = NO;
animationScale.delegate = self;
CGRect frame = self.backView.frame;
/*定点缩放的位置 锚点 如果(0,0)就是从左上角缩放,如果 (1,1)就是从右下角 */
self.backView.layer.anchorPoint = CGPointMake(0.9, 0.9);
self.backView.frame = frame;
[self.backView.layer addAnimation:animationScale forKey:@"scale-show-layer"];
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:0.2 animations:^{
weakSelf.backView.alpha = 1;
}];
}
-(void)backViewClickEvent{
/*
fromValue:开始帧
toValue:结束帧
removedOnCompletion:设置是否自动移除动画
fillMode:设置保存动画状态
-> kCAFillModeForwards:保持着动画结束后状态(removedOnCompletion = NO)
-> kCAFillModeBackwards:回到动画开始前状态(removedOnCompletion = NO)
-> kCAFillModeBoth:上面两个的合成
-> kCAFillModeRemoved:默认值 动画开始或结束都会回到最初状态
*/
CABasicAnimation *animationScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animationScale.duration = 0.2;
animationScale.repeatCount = 1;
animationScale.fromValue = @1.0;
animationScale.toValue = @0.0;
animationScale.fillMode = kCAFillModeBoth;
animationScale.removedOnCompletion = NO;
animationScale.delegate = self;
[self.backView.layer addAnimation:animationScale forKey:@"scale-remove-layer"];
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:0.2 animations:^{
weakSelf.backView.alpha = 0;
} completion:^(BOOL finished) {
[weakSelf.imageV removeFromSuperview];
weakSelf.imageV = nil;
[weakSelf.backView removeFromSuperview];
weakSelf.backView = nil;
}];
}
-(void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if ([anim isEqual:[self.backView.layer animationForKey:@"scale-show-layer"]]) {
[self.backView.layer removeAnimationForKey:@"scale-show-layer"];
}else if ([anim isEqual:[self.backView.layer animationForKey:@"scale-remove-layer"]]){
[self.backView.layer removeAnimationForKey:@"scale-remove-layer"];
[self.backView removeFromSuperview];
}
}
@end
网友评论