點(diǎn)擊彈出菜單按鈕

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

老規(guī)矩柒巫,先上代碼

//
//  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的動(dòng)畫時(shí)間
@property (nonatomic,copy)NSArray *delayArray;//所有button的動(dòng)畫延遲時(shí)間
@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];
    
    /**加載中心點(diǎn) */
    [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];
}

/**
 中心點(diǎn)的動(dòng)畫
 */
-(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.  首先先添加一個(gè)中心點(diǎn)的button父叙,添加上動(dòng)畫
2.計(jì)算要添加的button的數(shù)目,以及button的不同的位置乒裆,然后就可以給它添加動(dòng)畫璃俗,讓它顯示
3.對(duì)于button的消失奴璃,需要重新賦予動(dòng)畫,位置恢復(fù)到初始位置城豁,
4.這里我只計(jì)算了三個(gè)的這種情況苟穆,可以根據(jù)自己的情況訂制,為了方面一次性展示代碼唱星,沒(méi)有進(jìn)行封裝雳旅,其實(shí)稍加改動(dòng),可以進(jìn)行很好的封裝
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末间聊,一起剝皮案震驚了整個(gè)濱河市攒盈,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌哎榴,老刑警劉巖型豁,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件僵蛛,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡偷遗,警方通過(guò)查閱死者的電腦和手機(jī)墩瞳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)氏豌,“玉大人喉酌,你說(shuō)我怎么就攤上這事”么” “怎么了泪电?”我有些...
    開(kāi)封第一講書人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)纪铺。 經(jīng)常有香客問(wèn)我相速,道長(zhǎng),這世上最難降的妖魔是什么鲜锚? 我笑而不...
    開(kāi)封第一講書人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任突诬,我火速辦了婚禮,結(jié)果婚禮上芜繁,老公的妹妹穿的比我還像新娘旺隙。我一直安慰自己,他們只是感情好骏令,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布蔬捷。 她就那樣靜靜地躺著,像睡著了一般榔袋。 火紅的嫁衣襯著肌膚如雪周拐。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 52,441評(píng)論 1 310
  • 那天凰兑,我揣著相機(jī)與錄音妥粟,去河邊找鬼。 笑死吏够,一個(gè)胖子當(dāng)著我的面吹牛罕容,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播稿饰,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼露泊!你這毒婦竟也來(lái)了喉镰?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤惭笑,失蹤者是張志新(化名)和其女友劉穎侣姆,沒(méi)想到半個(gè)月后生真,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡捺宗,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年柱蟀,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蚜厉。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡长已,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出昼牛,到底是詐尸還是另有隱情术瓮,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布贰健,位于F島的核電站胞四,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏伶椿。R本人自食惡果不足惜辜伟,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望脊另。 院中可真熱鬧导狡,春花似錦、人聲如沸尝蠕。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)看彼。三九已至廊佩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間靖榕,已是汗流浹背标锄。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留茁计,地道東北人料皇。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像星压,于是被迫代替她去往敵國(guó)和親践剂。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容