美文网首页iOS
iOS 类似朋友圈分享的动画

iOS 类似朋友圈分享的动画

作者: 其实你懂De | 来源:发表于2016-12-02 14:21 被阅读221次

    最近公司说让写项目,可是一直还没动。闲的蛋疼呀,自己写了一个简单的类似朋友圈分享的动画,希望对大家有用吧!!先上个动画效果吧!!

    1.gif

    1.分享那个button大家都会吧,就不说了 。首先我们的自定一个View 将下边的白色View加到上面去,再将那两个button加到上面去。其他的就是动画效果了,话不多说上代码。
    2.自定义View (JYJPublishView)
    .h

    // 代理方法  判断点击了那个button
    @protocol JYJPublishViewDelegate <NSObject>
    
    - (void)didSelecteBtnWithBtntag:(NSInteger)tag;
    
    @end
    
    @interface JYJPusblishView : UIView
    @property (nonatomic, weak) id <JYJPublishViewDelegate> delegate;
    - (void)show;
    

    .m

    #define WIDTH [UIScreen mainScreen].bounds.size.width
    #define HEIGHT [UIScreen mainScreen].bounds.size.height
    #define ShareH 150
    
    
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
            [self createSubViews];
        }
        return self;
    }
    - (void)createSubViews {
        UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT - ShareH, WIDTH, ShareH)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [self addSubview:whiteView];
        
        UIButton *btn1 = [self btnAnimationWithFrame:CGRectMake(WIDTH / 4 - 30, HEIGHT - 80, 60, 60) imageName:@"img_wechat_logo" animationFrame:CGRectMake(WIDTH / 4 - 30, HEIGHT - 130, 60, 60) delay:0.0];
        btn1.tag = 1;
        [btn1 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        UIButton *btn2 = [self btnAnimationWithFrame:CGRectMake((WIDTH / 4) * 3 - 30, HEIGHT - 80, 60, 60) imageName:@"img_wechat_friend" animationFrame:CGRectMake((WIDTH / 4) * 3 - 30, HEIGHT - 130, 60, 60) delay:0.1];
        btn2.tag = 2;
        [btn2 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
        plus.frame = CGRectMake((WIDTH - 25) / 2, HEIGHT - 35, 25, 25);
        
        [plus setBackgroundImage:[UIImage imageNamed:@"close_share_icon"] forState:UIControlStateNormal];
        [plus addTarget:self action:@selector(cancelAnimation) forControlEvents:UIControlEventTouchUpInside];
        plus.tag = 3;
        [self addSubview:plus];
        [UIView animateWithDuration:0.2 animations:^{
            plus.transform = CGAffineTransformMakeRotation(M_PI_2);
        }];
        
        
    }
    - (void)show {
        UIWindow *keyWindown = [UIApplication sharedApplication].keyWindow;
        [keyWindown addSubview:self];
    }
    // 定义一个方法,减去一些重复的步骤,高内聚低耦合嘛!
    - (UIButton *)btnAnimationWithFrame:(CGRect)frame imageName:(NSString *)imageName animationFrame:(CGRect)aniFrame delay:(CGFloat)delay {
        UIButton *btn = [[UIButton alloc]init];
        btn.frame = frame;
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [self addSubview:btn];
        [UIView animateWithDuration:1 delay:delay usingSpringWithDamping:0.3 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            btn.frame = aniFrame;
        } completion:^(BOOL finished) {
            
        }];
        return btn;
        // usingSpringWithDamping:弹簧动画的阻尼值,也就是相当于摩擦力的大小 ,该属性的值0.0到1.0之间,越靠近0,阻尼越小,弹动的幅度越大,反之阻尼越大,弹动的幅度越小,如果大到一定程度,会出现不动的情况。
        // initialSpringVelocity:弹簧画的速率,或者说是动力。值越小的弹簧的动力越小,弹簧拉伸的幅度越小,反之动力越大,弹簧拉伸的幅度越大。这里注意的是,如果设置为0,表示忽略该属性,由动画持续时间和阻尼计算动画的效果。
        
    }
    
    - (void)BtnClick:(UIButton *)btn {
        for (NSInteger i = 0; i < self.subviews.count; i++) {
            UIView *view = self.subviews[i];
            if ([view isKindOfClass:[UIButton class]]) {
                [UIView animateWithDuration:0.3 delay:0.1 *i options:UIViewAnimationOptionTransitionCurlDown animations:^{
                    view.frame = CGRectMake(view.frame.origin.x, HEIGHT, 60, 60);
                } completion:^(BOOL finished) {
                    
                }];
            }
        }
        [self performSelector:@selector(removeView:) withObject:btn afterDelay:0.5];
    }
    
    - (void)removeView:(UIButton *)btn {
        [self removeFromSuperview];
        [self.delegate didSelecteBtnWithBtntag:btn.tag];
    }
    // 点击白色View范围之外的执行的方法
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint currentPosition = [touch locationInView:self];
        CGFloat deltaY  = currentPosition.y;
        if (deltaY < HEIGHT - ShareH) {
            [self cancelAnimation];
        }
    
    }
    - (void)cancelAnimation {
        UIButton *cancelButton = (UIButton *)[self viewWithTag:3];
        [UIView animateWithDuration:0.2 animations:^{
            cancelButton.transform = CGAffineTransformMakeRotation(-M_PI_2);
        }];
    
        for (NSInteger i = 0; i< self.subviews.count; i++ ) {
            UIView *view = self.subviews[i];
            if ([view isKindOfClass:[UIButton class]]) {
                [UIView animateWithDuration:0.3 delay:0.1 * i options:UIViewAnimationOptionTransitionCurlDown animations:^{
                    view.frame = CGRectMake(view.frame.origin.x, HEIGHT, 60, 60);
                } completion:^(BOOL finished) {
                    [self removeFromSuperview];
                }];
            }
        }
    }
    
    

    3.然后我们到VC里调用就可以了,别忘了引入JYJPublishView

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:btn];
        btn.frame = CGRectMake(100, 100, 100, 40);
        [btn setTitle:@"分享" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(ShareBtnClick) forControlEvents:UIControlEventTouchUpInside];
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (void)ShareBtnClick {
        JYJPusblishView *publishView = [[JYJPusblishView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        publishView.delegate = self;
        [publishView show];
    }
    - (void)didSelecteBtnWithBtntag:(NSInteger)tag {
        if (tag == 1) {
            NSLog(@"1");
        }else if (tag == 2) {
            NSLog(@"2");
        }else {
            NSLog(@"CLOSE");
        }
    }
    
    

    相关文章

      网友评论

      本文标题:iOS 类似朋友圈分享的动画

      本文链接:https://www.haomeiwen.com/subject/jdxqmttx.html