QQ粘性效果

2016-03-24  本文已影响154人  亡灵诅咒

动画分析

text.gif

1.搭建界面

-(void) setUp
{
    self.radius = self.bounds.size.width * 0.5;
    //设置圆角
    self.layer.cornerRadius = self.radius;
    self.layer.masksToBounds = YES;
}

2.平移控件

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self addGestureRecognizer:pan];
 //无法通过设置transform方法来获得移动中控件的frame位置的变化
    CGPoint transPoint = [pan translationInView:self];
    //平移控件
    self.transform = CGAffineTransformTranslate(self.transform, transPoint.x, transPoint.y);
    [pan setTranslation:CGPointZero inView:self];
    //利用center来改变控件的位置
    //获取偏移点
    CGPoint transPoint = [pan translationInView:self];
    //平移控件
    CGPoint center = self.center;
    center.x += transPoint.x;
    center.y += transPoint.y;
    self.center = center;
    //复原
    [pan setTranslation:CGPointZero inView:self];

3.终点的view

    //添加小圆
    UIView *smallRadius = [[UIView alloc] initWithFrame:self.bounds];
    smallRadius.layer.cornerRadius = self.radius;
    smallRadius.center = self.center;
    smallRadius.backgroundColor = [UIColor redColor];
//    [self.superview addSubview:smallRadius];此方法是将子控件添加到父控件上,并且让其在最上面显示
    //将添加的view添加到父控件上
    [self.superview insertSubview:smallRadius belowSubview:self];
//获得2圆心距离
-(CGFloat) distanceWithSmallView:(UIView *) smallView withBigView:(UIView *) bigView
{
    CGFloat distanceX = bigView.center.x - smallView.center.x;
    CGFloat distanceY = bigView.center.y - smallView.center.y;
    return sqrt(distanceX * distanceX + distanceY * distanceY);
}
    //获取小圆的半径
    CGFloat radius = self.radius - space;
    //修改小圆的尺寸
    self.smallView.bounds = CGRectMake(0, 0, radius * 2, radius * 2);
    self.smallView.layer.cornerRadius = radius;

4.不规则矩形的创建

-(UIBezierPath *) beziserWithSmallView:(UIView *) smallView withBigView:(UIView *)bigView
{
    //小 圆
    CGPoint small = smallView.center;
    CGFloat r = smallView.bounds.size.width * 0.5;
    //大 圆
    CGPoint big = bigView.center;
    CGFloat R = bigView.bounds.size.width * 0.5;    
    CGFloat distance = [self distanceWithSmallView:smallView withBigView:bigView];
    //两控件x,y轴上的偏移量
    CGFloat spaceX = big.x - small.x;
    CGFloat spaceY = big.y - small.y;
    //获取偏移角度
    CGFloat sinθ = spaceX / distance;
    CGFloat cosθ = spaceY / distance;
    //路径点
    CGPoint pointA = CGPointMake(small.x - r * cosθ, small.y + r * sinθ);
    CGPoint pointB = CGPointMake(small.x + r * cosθ, small.y - r * sinθ);
    CGPoint pointC = CGPointMake(big.x + R * cosθ, big.y - R * sinθ);
    CGPoint pointd = CGPointMake(big.x - R * cosθ, big.y + R * sinθ);
    //曲线控制点
    CGPoint ctlOne = CGPointMake(pointA.x + distance * 0.5 * sinθ, pointA.y + distance * 0.5 * cosθ);
    CGPoint ctltwo = CGPointMake(pointB.x + distance * 0.5 * sinθ, pointB.y + distance * 0.5 * cosθ);
    [self.path moveToPoint:pointA];
    [self.path addLineToPoint:pointB];
    //曲线控制点
    [self.path addQuadCurveToPoint:pointC controlPoint:ctlOne];
    [self.path addLineToPoint:pointd];
    //曲线控制点
    [self.path addQuadCurveToPoint:pointA controlPoint:ctltwo];
    return self.path;
}
-(CAShapeLayer *)shapeLayer
{
    if (!_shapeLayer) {
        CAShapeLayer *shapLayer = [CAShapeLayer layer];
        shapLayer.fillColor = [UIColor redColor].CGColor;
        //将形状图层渲染到主视图
        [self.superview.layer addSublayer:shapLayer];
        _shapeLayer = shapLayer;
    }
    return _shapeLayer;
}

动画实现

    if (distance >= dkSpace) { //超出范围,将起始view和形状图层隐藏
        self.smallView.hidden = YES;
//        self.shapeLayer.hidden = YES;//隐藏无法达到吸附效果
        [self.shapeLayer removeFromSuperlayer];
    }else if (distance < dkSpace && !self.smallView.hidden){ //只有当控件满足范围条件,并且起始view没有隐藏的条件下,才实现该效果
        //通过贝塞尔创建不规则曲线
        self.shapeLayer.path = [self beziserWithSmallView:self.smallView withBigView:self].CGPath;
    }
        if (distance  < dkSpace && !self.smallView.hidden) {//没有满足要求
            //返回起始点
            [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                //返回起始点
                self.frame = self.smallView.frame ;
                [self.shapeLayer removeFromSuperlayer];
            } completion:^(BOOL finished) {
                self.smallView.hidden = NO;
            }];
            //实现动画
            UIImageView *ani = [[UIImageView alloc]initWithFrame:self.bounds];
            ani.animationImages = self.icons;
            ani.animationRepeatCount = 1;
            ani.animationDuration = 1;
            [self addSubview:ani];
            //开始动画
            [ani startAnimating];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self removeFromSuperview];
            });

注意

 self.view.translatesAutoresizingMaskIntoConstraints = NO;
上一篇 下一篇

猜你喜欢

热点阅读