您的位置:首页 > 其它

旋转动画+悬浮按钮

2016-07-22 16:05 447 查看
在VC中的viewDidload中直接调用就OK,即:

 加入头文件在viewDidLoad里面加入

 ZYLXuanFuWuView *view = [[ZYLXuanFuWuView alloc] init];

 [self.view addSubview:view];

 就会添加会移动会旋转的悬浮动画了

在.h里面

#import <UIKit/UIKit.h>

typedef void (^DownLoadBlock) ();

@interface ZYLXuanFuWuView :
UIView

@property (nonatomic ,assign)CGPoint startPoint;//触摸起始点

@property (nonatomic ,assign)CGPoint endPoint;//触摸结束点

@property (nonatomic ,copy)DownLoadBlock downLoadBlock;

//=================

@property (nonatomic,strong)UIView *myTapView;

@property (nonatomic,strong)UIButton *myTapButton;

@property (nonatomic,strong)UIImageView *myTopImageView;

@end
在.m里面

#import "ZYLXuanFuWuView.h"

//主题颜色

#define MAINCOLOER [UIColor colorWithRed:105/255.0 green:149/255.0 blue:246/255.0
alpha:1]

#define kDownLoadWidth 60

#define kOffSet kDownLoadWidth / 2

@interface
ZYLXuanFuWuView()<UIDynamicAnimatorDelegate>

@property (nonatomic,retain)UIView *backgroundView;//背景视图

@property (nonatomic,retain)UIImageView *imageView;//图片视图

@property (nonatomic,retain)UIDynamicAnimator *animator;//物理仿真动画

//=======================

@property (nonatomic,retain)UIButton *tapButton;

@end

@implementation ZYLXuanFuWuView

/*

////=====

//#pragma mark 动画先顺时针旋转90度再逆时针旋转90度

////button有点击事件

//- (void)createMyLeftTopButtonView{

//    //    self.myTopImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MyNotesScreenW, MyNotesScreenH/3)];

//    //    self.myTopImageView.image = [UIImage imageNamed:@"3.jpg"];

//    //    [self.view addSubview:self.myTopImageView];

//    

//    self.myTapView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 50, 50)];

//    [self addSubview:self.myTapView];

//    CGRect frame = self.myTapView.bounds;

//    

//    UIImageView *imageView = [self createImageViewWithFrame:frame tag:75 named:@"1.jpg"];

//    //  定位点,不写默认是中心点

//    imageView.layer.anchorPoint = CGPointMake(28/45, 16/45);

//    imageView.frame = frame;

//    [self.myTapView addSubview:imageView];

//    

//    UIButton *myTopButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 15, 70, 70)];

//    //    myTopButton.backgroundColor = [UIColor yellowColor];

//    myTopButton.imageView.image = imageView.image;

//    [myTopButton addTarget:self action:@selector(tapRigthTopButton:) forControlEvents:UIControlEventTouchDown];

//    [self addSubview:myTopButton];

//}

//- (void)setAnimated{

//    id fromValue = [NSNumber numberWithFloat:-M_PI_4/2];

//    id toValue = [NSNumber numberWithFloat:M_PI_4/2];

//    UIImageView *imageView = [self.myTapView viewWithTag:75];

//    [self animationWithView:imageView keyPath:@"transform.rotation.z" fromValue:fromValue toValue:toValue];

//    

//}

//- (void)tapRigthTopButton:(UIButton *)tap{

//    //点击进入note界面

//    NSLog(@"点击了图片3");

//}

//- (UIImageView *)createImageViewWithFrame:(CGRect)frame tag:(NSInteger)tag named:(NSString *)name{

//    UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame];

//    imageView.tag = tag;

//    imageView.image = [UIImage imageNamed:name];

//    return imageView;

//}

//- (void)animationWithView:(UIView *)view keyPath:(NSString *)keyPath fromValue:(id)fromValue toValue:(id)toValue{

//    CAAnimation *animation = [self createAnimationWithKeyPath:keyPath fromValue:fromValue toValue:toValue];

//    [view.layer addAnimation:animation forKey:nil];

//}

//- (CAAnimation *)createAnimationWithKeyPath:(NSString *)keyPath fromValue:(id)fromValue toValue:(id)toValue{

//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];

//    animation.duration = 1.5; //
持续时间

//    CAMediaTimingFunction *mediaTiming = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

//    animation.timingFunction = mediaTiming;

//    animation.repeatCount = HUGE_VALF; //
重复次数

//    animation.fromValue =  fromValue;//
起始角度

//    animation.toValue = toValue; //
终止角度

//    animation.autoreverses = YES;

//    return animation;

//}

////========================================================

////========================================================

*/

-(instancetype)initWithFrame:(CGRect)frame{

    frame.size.width =kDownLoadWidth;

    frame.size.height =kDownLoadWidth;

    if (self = [superinitWithFrame:frame]) {

//初始化背景视图

        _backgroundView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.frame),CGRectGetHeight(self.frame))];

        _backgroundView.layer.cornerRadius =_backgroundView.frame.size.width
/2;

        _backgroundView.clipsToBounds =YES;

        _backgroundView.backgroundColor = [MAINCOLOERcolorWithAlphaComponent:0.7];

        _backgroundView.userInteractionEnabled =NO;

        [selfaddSubview:_backgroundView];

 //初始化图片背景视图

        UIView * imageBackgroundView = [[UIViewalloc]initWithFrame:CGRectMake(5,5,CGRectGetWidth(self.frame)
-10,CGRectGetHeight(self.frame) -10)];

        imageBackgroundView.layer.cornerRadius = imageBackgroundView.frame.size.width
/ 2;

        imageBackgroundView.clipsToBounds =
YES;

        imageBackgroundView.backgroundColor = [MAINCOLOERcolorWithAlphaComponent:0.8f];

        imageBackgroundView.userInteractionEnabled =NO;

        [self addSubview:imageBackgroundView];

 //初始化图片

        _imageView = [[UIImageViewalloc]initWithImage:[[UIImageimageNamed:@"1.jpg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

        _imageView.frame =CGRectMake(0,0,50,
50);

        _imageView.layer.cornerRadius =25;

        _imageView.layer.masksToBounds =YES;

        _imageView.center =CGPointMake(kDownLoadWidth
/2 ,
kDownLoadWidth / 2);

        [self addSubview:_imageView];

        //=======

        _tapButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.frame),CGRectGetHeight(self.frame))];

        _tapButton.backgroundColor = [UIColorgreenColor];

        _tapButton.layer.cornerRadius =_tapButton.frame.size.width/2;

        _tapButton.clipsToBounds =YES;

        _tapButton.userInteractionEnabled =NO;

        [_tapButtonaddTarget:selfaction:@selector(tapButton:)forControlEvents:UIControlEventTouchDown];

        [self addSubview:_tapButton];

        //======

//将正方形的view变成圆形

        self.layer.cornerRadius =kDownLoadWidth /2;

//开启呼吸动画

        [selfHighlightAnimation];

}

    return
self;

}

- (void)tapButton:(UIButton *)tap{

    NSLog(@"点击了悬浮动画的按钮");

   

}

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

//得到触摸点

    UITouch *startTouch = [touches
anyObject];

//返回触摸点坐标

    self.startPoint = [startTouchlocationInView:self.superview];

//移除之前的所有行为

    [self.animatorremoveAllBehaviors];

 }

//触摸移动

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

//得到触摸点

    UITouch *startTouch = [touches
anyObject];

//将触摸点赋值给touchView的中心点也就是根据触摸的位置实时修改view的位置

    self.center = [startTouchlocationInView:self.superview];

}

//结束触摸

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

//得到触摸结束点

    UITouch *endTouch = [touches
anyObject];

//返回触摸结束点

    self.endPoint = [endTouchlocationInView:self.superview];

//判断是否移动了视图 (误差范围5)

    CGFloat errorRange =
5;

    if (( self.endPoint.x -self.startPoint.x
>= -errorRange &&self.endPoint.x -self.startPoint.x
<= errorRange ) && (self.endPoint.y -self.startPoint.y
>= -errorRange &&self.endPoint.y -self.startPoint.y
<= errorRange )) {

    } else {

//移动

        self.center =self.endPoint;

//计算距离最近的边缘吸附到边缘停靠

        CGFloat superwidth =self.superview.bounds.size.width;

        CGFloat superheight =self.superview.bounds.size.height;

        CGFloat endX =
self.endPoint.x;

        CGFloat endY =
self.endPoint.y;

        CGFloat topRange = endY;//上距离

        CGFloat bottomRange = superheight - endY;//下距离

        CGFloat leftRange = endX;//左距离

        CGFloat rightRange = superwidth - endX;//右距离

//比较上下左右距离取出最小值

        CGFloat minRangeTB = topRange > bottomRange ? bottomRange : topRange;//获取上下最小距离

        CGFloat minRangeLR = leftRange > rightRange ? rightRange : leftRange;//获取左右最小距离

        CGFloat minRange = minRangeTB > minRangeLR ? minRangeLR : minRangeTB;//获取最小距离

//判断最小距离属于上下左右哪个方向并设置该方向边缘的point属性

        CGPoint minPoint;

        if (minRange == topRange) {

//上

            endX = endX - kOffSet <
0 ? kOffSet : endX;

            endX = endX + kOffSet > superwidth ? superwidth -kOffSet : endX;

            minPoint = CGPointMake(endX ,
0 + kOffSet);

        } else if(minRange == bottomRange){

 

//下

            endX = endX - kOffSet <
0 ? kOffSet : endX;

            endX = endX + kOffSet > superwidth ? superwidth -kOffSet : endX;

            minPoint = CGPointMake(endX , superheight -kOffSet);

 

        } else if(minRange == leftRange){

//左

            endY = endY - kOffSet <
0 ? kOffSet : endY;

            endY = endY + kOffSet > superheight ? superheight -kOffSet : endY;

            minPoint = CGPointMake(0 +kOffSet , endY);

        } else if(minRange == rightRange){

//右

            endY = endY - kOffSet <
0 ? kOffSet : endY;

            endY = endY + kOffSet > superheight ? superheight -kOffSet : endY;

            minPoint = CGPointMake(superwidth -kOffSet , endY);

        }

//添加吸附物理行为

        UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavioralloc]initWithItem:selfattachedToAnchor:minPoint];

        [attachmentBehavior setLength:0];

        [attachmentBehavior setDamping:0.1];

        [attachmentBehavior setFrequency:5];

        [self.animatoraddBehavior:attachmentBehavior];

        }

}

#pragma mark ---UIDynamicAnimatorDelegate

-(void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator{}

#pragma mark ---LazyLoading

- (UIDynamicAnimator *)animator{

    if (!_animator) {

    //
创建物理仿真器(ReferenceView :
仿真范围)

        _animator = [[UIDynamicAnimatoralloc]initWithReferenceView:self.superview];

    //设置代理

        _animator.delegate =self;

}

    return_animator;

}

#pragma mark ---BreathingAnimation
呼吸动画

- (void)HighlightAnimation{

    __block
typeof(self) Self =self;

    [UIViewanimateWithDuration:1.5fanimations:^{

        Self.imageView.transform =CGAffineTransformRotate(Self.imageView.transform,
-M_PI_2);

//        Self.imageView.transform = CGAffineTransformRotate(Self.imageView.image, M_PI_2);

        Self.backgroundView.backgroundColor = [Self.backgroundView.backgroundColorcolorWithAlphaComponent:0.1f];

        

    } completion:^(BOOL finished) {

        [Self DarkAnimation];

    }];

}

- (void)DarkAnimation{

    __block
typeof(self) Self =self;

    [UIViewanimateWithDuration:1.5fanimations:^{

        Self.imageView.transform =CGAffineTransformRotate(Self.imageView.transform,M_PI_2);

        Self.backgroundView.backgroundColor = [Self.backgroundView.backgroundColorcolorWithAlphaComponent:0.6f];

        

    } completion:^(BOOL finished) {

        [Self HighlightAnimation];

    }];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: