点击弹出菜单按钮

4月-01-2017 11-00-59.gif

老规矩,先上代码

//
//  PopButtonView.m
//  MyTest
//
//  Created by 丁祥 on 2017/3/28.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "PopButtonView.h"
#import "TestThirdViewController.h"
#define centerPointX 180
#define centerPointY 200
#define DxCovertAngelToRadian(x) ((x)*M_PI)/180

@interface PopButtonView()

@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UIButton *centerButton;
@property (nonatomic,assign)NSInteger buttonCount;
@property (nonatomic,strong)NSMutableArray *buttonArray;//所有的button
@property (nonatomic,strong)NSMutableArray *centerArray;//所有button的位置
@property (nonatomic,copy)NSArray *durtionArray;//所有button的动画时间
@property (nonatomic,copy)NSArray *delayArray;//所有button的动画延迟时间
@property (nonatomic,copy)NSArray *imageArrary;
@property (nonatomic,assign)CGFloat disdance;
@property (nonatomic,assign)BOOL state;


@end

@implementation PopButtonView

- (instancetype)initWithFrame:(CGRect)frame
{

    self =[super initWithFrame:frame];
    if (self) {
        
        self.buttonCount =3;
        self.disdance =100;
        self.state =YES;
        self.backgroundColor =[UIColor colorWithWhite:1 alpha:1];
        [self applyUI];

    }
    
    return self;
    
}

#pragma mark -init method
- (void)applyUI
{
    
     _centerButton =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    
    _centerButton.center =CGPointMake(centerPointX, centerPointY);
    
    [_centerButton setImage:[UIImage imageNamed:@"custom_center"] forState:UIControlStateNormal];
    
    [_centerButton addTarget:self action:@selector(displayButton:) forControlEvents:UIControlEventTouchUpInside];
    
    /**加载中心点 */
    [self addSubview:_centerButton];
    
    [self loadButton];
    
}



- (void)loadButton{
    
    for (int i=0; i<self.buttonCount; i++) {
        
        UIButton *button =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        
        button.center  =CGPointMake(-100, -100);
        
        [button setImage:[self.imageArrary objectAtIndex:i] forState:UIControlStateNormal];
        
        [self addSubview:button];
        
        [self.buttonArray addObject:button];
        
    }
    

}

#pragma mark -method

- (void)displayButton:(UIButton *)sender
{
    
    _state =!_state;
    
    [self displayImage];

    if (!_state) {
        [self.buttonArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [self displayButtonWithButton:obj withCenter:CGPointFromString(self.centerArray[idx]) withDuration:[self.durtionArray[idx] floatValue] withDelay:[self.delayArray[idx] floatValue]];
            
        }];
    }else{
    
            [self.buttonArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                [self dismissButtonWithButton:obj withCenter:CGPointFromString(self.centerArray[idx]) withDurantion:[self.durtionArray[idx] floatValue] withDelay:[self.durtionArray[idx] floatValue]];
                
            }];
    
    }
    
    
}

#pragma mark --Animation

-(void)displayButtonWithButton:(UIButton *)button withCenter:(CGPoint)buttonPoint withDuration:(CFTimeInterval)duration withDelay:(CFTimeInterval)delay
{
    button.center =buttonPoint;
    CAKeyframeAnimation *buttonAnimation =[CAKeyframeAnimation animation];
    buttonAnimation.keyPath =@"transform";
    
    CAKeyframeAnimation *keysAnimal =[CAKeyframeAnimation animation];
    
    keysAnimal.keyPath =@"transform";
    keysAnimal.duration =duration;
    NSValue *value1 =[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1)];
    NSValue *value2 =[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.4, 1.4, 1)];
    NSValue *value3 =[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
    keysAnimal.values =@[value1,value2,value3];
    keysAnimal.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    keysAnimal.keyTimes = @[[NSNumber numberWithFloat:0.0f],[NSNumber numberWithFloat:1-delay],[NSNumber numberWithFloat:1.0f]];
    [button.layer addAnimation:keysAnimal forKey:nil];

}

- (void)dismissButtonWithButton:(UIButton *)button withCenter:(CGPoint)local withDurantion:(CFTimeInterval)durantion withDelay:(CFTimeInterval)delay
{
    
    CAKeyframeAnimation *keysAnimal =[CAKeyframeAnimation animation];
    
    keysAnimal.duration =durantion *delay;
    keysAnimal.keyPath =@"transform.rotation.z";
    NSNumber *number1 =[NSNumber numberWithFloat:0.f];
    NSNumber *number2 =[NSNumber numberWithFloat:2*M_PI];
    keysAnimal.values =@[number1,number2,number1];
    keysAnimal.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    CAKeyframeAnimation *shrink = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    shrink.duration = durantion * (3 - delay);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, local.x, local.y);
    CGPathAddLineToPoint(path, NULL, local.x -20.f, local.y -20.f);
    CGPathAddLineToPoint(path, NULL, centerPointX, centerPointY);
    shrink.path = path;
    CGPathRelease(path);
    
    CAAnimationGroup *totalAnimation = [CAAnimationGroup animation];
    totalAnimation.duration = 3.0f;
    totalAnimation.animations = @[keysAnimal,shrink];
    totalAnimation.fillMode = kCAFillModeForwards;
    totalAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    button.center =CGPointMake(-100, -100);
    [button.layer  addAnimation:totalAnimation forKey:nil];
}

/**
 中心点的动画
 */
-(void)displayImage{
    
    CAKeyframeAnimation *keysAnimal =[CAKeyframeAnimation animation];
    
    keysAnimal.keyPath =@"transform";
    keysAnimal.duration =1.0;
    NSValue *value1 =[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
    NSValue *value2 =[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1)];
    NSValue *value3 =[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.4, 1.4, 1)];
    keysAnimal.values =@[value1,value3,value1,value2,value1];
    keysAnimal.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.centerButton.layer addAnimation:keysAnimal forKey:nil];
    
}
#pragma mark -初始化位置

#pragma mark -懒加载

-(NSMutableArray *)centerArray
{
    if (!_centerArray) {
        
        _centerArray =[NSMutableArray array];
        CGFloat sinX =sinf(DxCovertAngelToRadian(30.f));
        CGFloat cosX =cosf(DxCovertAngelToRadian(30.f));
        
        CGPoint point1 =CGPointMake(centerPointX- _disdance*cosX, centerPointY -_disdance*sinX);
        CGPoint point2 =CGPointMake(centerPointX+ _disdance*cosX, centerPointY -_disdance*sinX);
        CGPoint point3 =CGPointMake(centerPointX, centerPointY +_disdance);
        
        [self.centerArray addObject:NSStringFromCGPoint(point1)];
        [self.centerArray addObject:NSStringFromCGPoint(point2)];
        [self.centerArray addObject:NSStringFromCGPoint(point3)];
    }
    
    return _centerArray;

}
- (NSMutableArray *)buttonArray
{
    if (!_buttonArray) {
        
        _buttonArray =[NSMutableArray array];
    }
    
    return _buttonArray;


}


- (NSArray *)durtionArray
{
    if (!_durtionArray) {
        
        _durtionArray =@[@0.35,@0.40,@0.45,@0.50,@0.55,@0.60];
    }
    
    return _durtionArray;



}

- (NSArray *)delayArray
{
    if (!_delayArray) {
        _delayArray =@[@0.50,@0.55,@0.60,@0.65,@0.70,@0.75];
    }
    
    return _delayArray;
    
    
}

- (NSArray *)imageArrary
{
    if (!_imageArrary) {
        _imageArrary =@[[UIImage imageNamed:@"custom_1"],[UIImage imageNamed:@"custom_1"],[UIImage imageNamed:@"custom_1"]];
    }
    
    return _imageArrary;



}

@end

1.  首先先添加一个中心点的button,添加上动画
2.计算要添加的button的数目,以及button的不同的位置,然后就可以给它添加动画,让它显示
3.对于button的消失,需要重新赋予动画,位置恢复到初始位置,
4.这里我只计算了三个的这种情况,可以根据自己的情况订制,为了方面一次性展示代码,没有进行封装,其实稍加改动,可以进行很好的封装
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容