動(dòng)畫(huà)分析
- 當(dāng)前控件既可以顯示圖片,有可以顯示文字,那么我們就可以通過(guò)按鈕來(lái)最為當(dāng)前的控件.
- 當(dāng)拖動(dòng)控件,當(dāng)前控件尺寸不會(huì)改變,起始位置處會(huì)有一個(gè)同尺寸的控件隨著偏移的距離,逐漸變小.起始位置的控件只是簡(jiǎn)單的用來(lái)顯示,所以這里可以用一個(gè)View來(lái)完成.而2控件中間的紐帶我們可以通過(guò)圖層來(lái)實(shí)現(xiàn).
1.搭建界面
- 從storyboard中創(chuàng)建一個(gè)按鈕,并且自定義一個(gè)繼承自UIButton的類(lèi),修改按鈕的class
- 在初始化方法中,初始化我們自定義按鈕的屬性
- 考慮到按鈕分裝重用,將初始化方法抽取出來(lái),分別在
awakeFromNib
和initWithFrame
中實(shí)現(xiàn)
-(void) setUp
{
self.radius = self.bounds.size.width * 0.5;
//設(shè)置圓角
self.layer.cornerRadius = self.radius;
self.layer.masksToBounds = YES;
}
2.平移控件
- 在初始化方法中,給控件添加平移手勢(shì),實(shí)現(xiàn)方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
- 在平移手勢(shì)的方法中,獲取當(dāng)前的偏移點(diǎn)來(lái)移動(dòng)控件
//無(wú)法通過(guò)設(shè)置transform方法來(lái)獲得移動(dòng)中控件的frame位置的變化
CGPoint transPoint = [pan translationInView:self];
//平移控件
self.transform = CGAffineTransformTranslate(self.transform, transPoint.x, transPoint.y);
[pan setTranslation:CGPointZero inView:self];
//利用center來(lái)改變控件的位置
//獲取偏移點(diǎn)
CGPoint transPoint = [pan translationInView:self];
//平移控件
CGPoint center = self.center;
center.x += transPoint.x;
center.y += transPoint.y;
self.center = center;
//復(fù)原
[pan setTranslation:CGPointZero inView:self];
3.終點(diǎn)的view
- 添加一個(gè)view在起始位置處.平移手勢(shì)方法調(diào)用頻繁,所以選擇在初始化方法中添加
//添加小圓
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];
- 自定義一個(gè)形變系數(shù),通過(guò)兩圓的圓心距,獲得起始圓的形變尺寸
//獲得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.不規(guī)則矩形的創(chuàng)建
- 通過(guò)形狀圖層,將不規(guī)則的矩形渲染到視圖
- 創(chuàng)建貝塞爾曲線
-(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;
//路徑點(diǎn)
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θ);
//曲線控制點(diǎn)
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];
//曲線控制點(diǎn)
[self.path addQuadCurveToPoint:pointC controlPoint:ctlOne];
[self.path addLineToPoint:pointd];
//曲線控制點(diǎn)
[self.path addQuadCurveToPoint:pointA controlPoint:ctltwo];
return self.path;
}
- 聲明一個(gè)CAShapeLayer屬性,并且懶加載
-(CAShapeLayer *)shapeLayer
{
if (!_shapeLayer) {
CAShapeLayer *shapLayer = [CAShapeLayer layer];
shapLayer.fillColor = [UIColor redColor].CGColor;
//將形狀圖層渲染到主視圖
[self.superview.layer addSublayer:shapLayer];
_shapeLayer = shapLayer;
}
return _shapeLayer;
}
動(dòng)畫(huà)實(shí)現(xiàn)
if (distance >= dkSpace) { //超出范圍,將起始view和形狀圖層隱藏
self.smallView.hidden = YES;
// self.shapeLayer.hidden = YES;//隱藏?zé)o法達(dá)到吸附效果
[self.shapeLayer removeFromSuperlayer];
}else if (distance < dkSpace && !self.smallView.hidden){ //只有當(dāng)控件滿足范圍條件,并且起始view沒(méi)有隱藏的條件下,才實(shí)現(xiàn)該效果
//通過(guò)貝塞爾創(chuàng)建不規(guī)則曲線
self.shapeLayer.path = [self beziserWithSmallView:self.smallView withBigView:self].CGPath;
}
if (distance < dkSpace && !self.smallView.hidden) {//沒(méi)有滿足要求
//返回起始點(diǎn)
[UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//返回起始點(diǎn)
self.frame = self.smallView.frame ;
[self.shapeLayer removeFromSuperlayer];
} completion:^(BOOL finished) {
self.smallView.hidden = NO;
}];
//實(shí)現(xiàn)動(dòng)畫(huà)
UIImageView *ani = [[UIImageView alloc]initWithFrame:self.bounds];
ani.animationImages = self.icons;
ani.animationRepeatCount = 1;
ani.animationDuration = 1;
[self addSubview:ani];
//開(kāi)始動(dòng)畫(huà)
[ani startAnimating];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self removeFromSuperview];
});
注意
- 應(yīng)為我們是在storyboard中拖得控件,所以默認(rèn)是會(huì)將Autoresizing轉(zhuǎn)自動(dòng)布局,需要利用代碼來(lái)設(shè)置取消
self.view.translatesAutoresizingMaskIntoConstraints = NO;