IOS UI动画与手势的基本使用

来源:互联网 发布:惠普2025网络设置 编辑:程序博客网 时间:2024/04/28 18:44
#import "UIViewAnimation.h"
#import "UIImageAnimatioController.h"

@interface UIViewAnimation ()
{

    UIView * _blackRect;
}

@end

@implementation UIViewAnimation

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"动画";
    
    UIBarButtonItem * nextPage = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(pressedNext)];
    self.navigationItem.rightBarButtonItem = nextPage;
    
    UIBarButtonItem * backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:nil];//上一个视图控制器决定下一个视图控制器的返回按钮
    self.navigationItem.backBarButtonItem = backItem;
    
    
    _blackRect = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    
    _blackRect.backgroundColor = [UIColor blackColor];
    _blackRect.layer.cornerRadius = 3;
    [self.view addSubview:_blackRect];
    
#pragma mark - 添加点击手势识别器
    UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(precessTap)];
    tapGR.numberOfTapsRequired = 1;//设置点击次数
    tapGR.numberOfTouchesRequired = 1;//设置手指个数
    [_blackRect addGestureRecognizer:tapGR];//添加
    
#pragma mark - 添加滑动手势识别器
    UISwipeGestureRecognizer * swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(precessTap:)];
    
    swipeGR.direction = UISwipeGestureRecognizerDirectionUp;//设置手势识别器滑动方向(默认是右滑)
    swipeGR.numberOfTouchesRequired = 1;//设置手指数量
    
    [self.view addGestureRecognizer:swipeGR];
    
    
}

- (void)precessTap:(UIGestureRecognizer *)sender
{
    if ([sender isKindOfClass:[UITapGestureRecognizer class]]) {
        
        NSLog(@"是点击手势");
    }else if ([sender isKindOfClass:[UISwipeGestureRecognizer class]]) {
        NSLog(@"是滑动手势");
    }

}





- (void)pressedNext
{
    UIImageAnimatioController * imageAnimation = [[UIImageAnimatioController alloc] init];
    
    [self.navigationController pushViewController:imageAnimation animated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
    
}

- (void)precessTap
{
    NSLog(@"点击了黑块!");
    [self posstionAnimation];

}

#pragma mark - View Animation

//位移动画(移动到中间的动画)
- (void)posstionAnimation
{
//    //开始一个动画
//    [UIView beginAnimations:@"position" context:nil];//动画名:position
//    //动画的设置
//    [UIView setAnimationDuration:0.3];  //设置动画持续时间
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //设置这个动画的变化规律(效果)这里设置为慢入慢出
//    
//    //动画内容(移动中心点)
////    [UIView setAnimationRepeatAutoreverses:YES];//是否进行逆行执行动画
////    [UIView setAnimationRepeatCount:5];//执行次数
//    [UIView setAnimationDelegate:self];  //设置委托对象为自己,让自己再去调用接下来的方法
//    [UIView  setAnimationDidStopSelector:@selector(stopPossitionAnimation:)];//该动画执行结束后执行的方法
//    _blackRect.center = CGPointMake(CGRectGetMidX(self.view.bounds), 280);
//    
//    //动画开始执行
//    [UIView commitAnimations];
    
    //动画block
    [UIView animateWithDuration:0.5 animations:^{
        //这里写动画效果
        _blackRect.center = CGPointMake(CGRectGetMidX(self.view.bounds), 280);
    }completion:^(BOOL finished) {
        //这里写动画执行完后做什么
        [self stopPossitionAnimation:@"position"];
    }];
    
}

//缩放动画
- (void)scaleAnimation
{
    [UIView beginAnimations:@"scale" context:nil];
    [UIView setAnimationDuration:1];
    
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(stopPossitionAnimation:)];
    _blackRect.transform = CGAffineTransformScale(_blackRect.transform, 1.5, 1.5);//放大1.8倍
    
    [UIView commitAnimations];
}

//旋转动画
- (void)rotationAnimation
{
    [UIView beginAnimations:@"rotate" context:nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(stopPossitionAnimation:)];
    
    _blackRect.transform = CGAffineTransformRotate(_blackRect.transform, M_PI);//旋转180度
    _blackRect.transform = CGAffineTransformRotate(_blackRect.transform, M_PI);//旋转180度
    _blackRect.transform = CGAffineTransformRotate(_blackRect.transform, M_PI);//旋转180度
    _blackRect.transform = CGAffineTransformRotate(_blackRect.transform, M_PI);//旋转180度
    _blackRect.transform = CGAffineTransformRotate(_blackRect.transform, M_PI);//旋转180度
    _blackRect.transform = CGAffineTransformRotate(_blackRect.transform, M_PI);//旋转180度
    
    [UIView commitAnimations];
}

//颜色变化动画
- (void)colorChangeAnimation
{
    [UIView beginAnimations:@"colorChange" context:nil];
    [UIView setAnimationDuration:0.6];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(stopPossitionAnimation:)];
    _blackRect.backgroundColor = [UIColor yellowColor];
    
    [UIView commitAnimations];
}

- (void)endAnimation
{
    [UIView beginAnimations:@"end" context:nil];
    [UIView setAnimationDuration:0.7];
    
    _blackRect.backgroundColor = [UIColor blackColor];
    _blackRect.center = CGPointMake(100, 100);
    _blackRect.transform = CGAffineTransformIdentity;//变换到原来的大小
    
//    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:_blackRect cache:NO];//翻页效果
    
    [UIView commitAnimations];

}



- (void)stopPossitionAnimation:(NSString *)animationID
{
    NSLog(@"位移动画结束了!");
    if ([animationID isEqualToString:@"position"]) {
        //位移动画结束后调用缩放动画
        [self scaleAnimation];
    }else if ([animationID isEqualToString:@"scale"]) {
        //缩放动画结束后调用旋转动画
        [self rotationAnimation];
    }else if ([animationID isEqualToString:@"rotate"]) {
        //旋转结束后调用颜色变化动画
        [self colorChangeAnimation];
    }else if ([animationID isEqualToString:@"colorChange"]) {
        //颜色变化后调用结束动画
        [self endAnimation];
    }
}

@end
0 0