#import
@interfaceBageValueButton :UIButton
@end
#import "BageValueButton.h"
@interface BageValueButton ()
@property (nonatomic, strong) CAShapeLayer *shapL;
@property (nonatomic, strong) UIView *smallCircle;
@property (nonatomic, strong) UIView *bigCircle;
@end
@implementationBageValueButton
-(CAShapeLayer *)shapL{
if(_shapL==nil) {
CAShapeLayer *shapL = [CAShapeLayer layer];
[self.superview.layerinsertSublayer:shapLatIndex:0];
shapL.fillColor = [UIColor redColor].CGColor;
_shapL= shapL;
}
return _shapL;
}
-(instancetype)initWithFrame:(CGRect)frame{
self= [superinitWithFrame:frame];
if(self) {
[selfsetUp];
}
return self;
}
-(void)awakeFromNib{
[selfsetUp];
//添加手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer *)pan{
//拖动
CGPointtransP = [pantranslationInView:self];
//transform并没有修改center,它修改的是frame
//self.transform = CGAffineTransformTranslate(self.transform, transP.x, transP.y);
CGPointcenter =self.center;
center.x+= transP.x;
center.y+= transP.y;
self.center= center;
//复位
[pansetTranslation:CGPointZero inView:self];
CGFloat distance = [self distanceWithSmallCircle:self.smallCircle bigCircle:self];
//让小圆半径根据距离的增大,半径在减小
CGFloat smallR = self.bounds.size.width * 0.5;
smallR -= distance /10.0;
self.smallCircle.bounds=CGRectMake(0,0, smallR *2, smallR *2);
self.smallCircle.layer.cornerRadius = smallR;
UIBezierPath *path = [self pathWithSmallCircle:self.smallCircle BigCircle:self];
//形状图层
if(self.smallCircle.hidden==NO) {
self.shapL.path= path.CGPath;
}
if(distance >60) {
//让小圆隐藏,让路径隐藏
self.smallCircle.hidden=YES;
[self.shapL removeFromSuperlayer];
}
if (pan.state == UIGestureRecognizerStateEnded) {
//判断结束时,距离是否大于60,
//大于60,让按钮消失,
if(distance <60) {
//小于60复位
[self.shapL removeFromSuperlayer];
self.center=self.smallCircle.center;
self.smallCircle.hidden=NO;
}else{
//播放一个动画消失
UIImageView*imageView = [[UIImageViewalloc]initWithFrame:self.bounds];
NSMutableArray*imageArray = [NSMutableArrayarray];
for(inti =0; i <8; i ++) {
UIImage*image = [UIImageimageNamed:[NSStringstringWithFormat:@"name%d",i +1]];
[imageArrayaddObject:image];
}
imageView.animationImages= imageArray;
imageView.animationDuration=1;
[imageViewstartAnimating];
[selfaddSubview:imageView];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[selfremoveFromSuperview];
});
}
}
}
//给定两个圆,细述一个不规则的路径
-(UIBezierPath*)pathWithSmallCircle:(UIView*)smallCircle BigCircle:(UIView*)bigCircle{
CGFloatx1 = smallCircle.center.x;
CGFloaty1 = smallCircle.center.y;
CGFloatx2 = bigCircle.center.x;
CGFloaty2 = bigCircle.center.y;
CGFloatd = [selfdistanceWithSmallCircle:smallCirclebigCircle:bigCircle];
if(d <=0) {
returnnil;
}
CGFloatcosθ = (y2 - y1) / d;
CGFloatsinθ = (x2 - x1) / d;
CGFloatr1 = smallCircle.bounds.size.width*0.5;
CGFloatr2 = bigCircle.bounds.size.width*0.5;
//描述点
//A点
CGPointpointA =CGPointMake(x1 - r1 * cosθ, y1 + r1 * sinθ);
CGPointpointB =CGPointMake(x1 + r1 * cosθ, y1 - r1 * sinθ);
CGPointpointC =CGPointMake(x2 + r2 * cosθ, y2 - r2 * sinθ);
CGPointpointD =CGPointMake(x2 - r2 * cosθ, y2 + r2 * sinθ);
CGPointpoint0 =CGPointMake(pointA.x+ d *0.5* sinθ, pointA.y + d *0.5* cosθ);
CGPointpointP =CGPointMake(pointB.x+ d *0.5* sinθ, pointB.y + d *0.5* cosθ);
UIBezierPath *path = [UIBezierPath bezierPath];
//AB
[pathmoveToPoint:pointA];
[pathaddLineToPoint:pointB];
//BC(曲线)
[pathaddQuadCurveToPoint:pointC controlPoint:pointP];
//CD
[pathaddLineToPoint:pointD];
//DA(曲线)
[pathaddQuadCurveToPoint:pointA controlPoint:point0];
returnpath;
}
-(void)setUp{
//设置圆角
self.layer.cornerRadius = self.bounds.size.width * 0.5;
//设置形状的填充颜色
[self setBackgroundColor:[UIColor redColor]];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont systemFontOfSize:12];
//添加小圆
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
//添加
UIView*smallCircle = [[UIViewalloc]init];
smallCircle.frame=self.frame;
smallCircle.backgroundColor = self.backgroundColor;
smallCircle.layer.cornerRadius = self.layer.cornerRadius;
self.smallCircle= smallCircle;
//把一个UIView添加到指定位置
[self.superview insertSubview:smallCircle belowSubview:self];
}
//取消高亮状态
-(void)setHighlighted:(BOOL)highlighted{
}
//求两个圆之间的距离
-(CGFloat)distanceWithSmallCircle:(UIView*)smallCircle bigCircle:(UIView*)bigCircle{
//x轴方法向的偏移量
CGFloatoffsetX = bigCircle.center.x- smallCircle.center.x;
//y轴方法向的偏移量
CGFloatoffsetY = bigCircle.center.y- smallCircle.center.y;
returnsqrt(offsetX * offsetX + offsetY * offsetY);
}
@end
网友评论