iOS 悬浮按钮的实现

项目中为了语音功能的更好体验,加了个悬浮按钮,网上找了好久,最终集成了一个悬浮按钮,效果还不错。

参考Demo

可拖动的按钮
按钮的拖动和吸附在父控件边缘

悬浮按钮.gif

自定义个UIButton


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

//触摸-清扫
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    MoveEnabled = NO;
    [super touchesBegan:touches withEvent:event];
    if (!MoveEnable) {
        return;
    }
    UITouch *touch = [touches anyObject];
    beginpoint = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    MoveEnabled = YES;//单击事件可用
    
    if (!MoveEnable) {
        return;
    }
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self];
    //偏移量
    float offsetX = currentPosition.x - beginpoint.x;
    float offsetY = currentPosition.y - beginpoint.y;
    //移动后的中心坐标
    self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
    
    //x轴左右极限坐标
    if (self.center.x > (self.superview.frame.size.width-self.frame.size.width/2)) {
        CGFloat x = self.superview.frame.size.width-self.frame.size.width/2;
        self.center = CGPointMake(x, self.center.y + offsetY);
    }else if (self.center.x < self.frame.size.width/2){
        CGFloat x = self.frame.size.width/2;
        self.center = CGPointMake(x, self.center.y + offsetY);
    }
    
    //y轴上下极限坐标
    if (self.center.y > (self.superview.frame.size.height-self.frame.size.height/2)) {
        CGFloat x = self.center.x;
        CGFloat y = self.superview.frame.size.height-self.frame.size.height/2;
        self.center = CGPointMake(x, y);
    }else if (self.center.y <= self.frame.size.height/2){
        CGFloat x = self.center.x;
        CGFloat y = self.frame.size.height/2;
        self.center = CGPointMake(x, y);
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!MoveEnable) {
        return;
    }
    if (self.center.x >= self.superview.frame.size.width/2) {//向右侧移动
        //偏移动画
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationDelegate:self];
        self.frame=CGRectMake(335.f,self.center.y-20, 40.f,40.f);
        //提交UIView动画
        [UIView commitAnimations];
    }else{//向左侧移动
        
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationDelegate:self];
        self.frame=CGRectMake(0.f,self.center.y-20, 40.f,40.f);
        //提交UIView动画
        [UIView commitAnimations];
        
    }
    
    //不加此句话,UIButton将一直处于按下状态
    [super touchesEnded: touches withEvent: event];
    
}

//外界因素取消touch事件,如进入电话
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

MyButton实现

#import "ViewController.h"

#import "MyButton.h"
@interface ViewController ()
{
    UIView *tabBarView;
    MyButton *myButton;
    BOOL flag; //控制tabbar的显示与隐藏标志

}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bj_bg"]];
    


    
}
//做了修改 设置tab bar
- (void)addCustomElements
{
    myButton = [MyButton buttonWithType:UIButtonTypeCustom];
    myButton.MoveEnable = YES;
    myButton.frame = CGRectMake(335, 300, 40, 40);
    
    //TabBar上按键图标设置
    [myButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"40.png"]] forState:UIControlStateNormal];
    [myButton setTag:10];
    flag = NO;//控制tabbar的显示与隐藏标志 NO为隐藏
    
    [myButton addTarget:self action:@selector(tabbarbtn:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:myButton];
    
    [self _initTabBar];
}

//初始化tabbar
-(void)_initTabBar
{
    //tab bar view  始终居中显示    
    tabBarView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-100, self.view.frame.size.height/2-100, 200 , 200)] ;
    
    //view 设置半透明 圆角样式
    tabBarView.layer.cornerRadius = 10;//设置圆角的大小
    tabBarView.layer.backgroundColor = [[UIColor blackColor] CGColor];

    
    tabBarView.alpha = 0.8f;//设置透明
    tabBarView.layer.masksToBounds = YES;
    
    [self.view addSubview:tabBarView];
    
    //循环设置tabbar上的button
//    NSArray *imgNames = [[NSArray alloc]initWithObjects:@"download.png",@"block.png",@"bluetooth.png",@"file.png", nil];
    NSArray *tabTitle = [[NSArray alloc]initWithObjects:@"download",@"block",@"bluetooth",@"file", nil];
    
    for (int i=0; i<4; i++) {
        CGRect rect;
        rect.size.width = 60;
        rect.size.height = 60;
        switch (i) {
            case 0:
                rect.origin.x = 100-30;
                rect.origin.y = 40-35;
                break;
            case 1:
                rect.origin.x = 375/2-50;
                rect.origin.y = 100-30;
                break;
            case 2:
                rect.origin.x = 100-30;
                rect.origin.y = 375/2-50;
                break;
            case 3:
                rect.origin.x = 40-35;
                rect.origin.y = 100-30;
                break;
        }
        
        
              //设置每个tabView
        UIView *tabView = [[UIView alloc]initWithFrame:rect];
        [tabBarView addSubview:tabView];
        
        

        //设置tabView的图标
        UIButton *tabButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tabButton.frame = CGRectMake(15, 0, 30, 30);
        [tabButton setBackgroundImage:[UIImage imageNamed:[tabTitle objectAtIndex:i]] forState:UIControlStateNormal];
        [tabButton setTag:i];
        [tabButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [tabView addSubview:tabButton];
        
        //设置标题
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 35, 60, 15)];
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.textAlignment = 1;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.text = [tabTitle objectAtIndex:i];
        [tabView addSubview:titleLabel];
    }
    
    [tabBarView setHidden:YES];
}

//显示 隐藏tabbar
- (void)tabbarbtn:(MyButton*)btn
{
    //在移动的时候不触发点击事件
    if (!btn.MoveEnabled) {
        if(!flag){
            tabBarView.hidden = NO;
            flag = YES;
        }else{
            tabBarView.hidden = YES;
            flag = NO;
        }
    }
    
}

- (void)buttonClicked:(id)sender
{
    NSLog(@"%ld",[sender tag]);
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self addCustomElements];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

高仿 iPhoneTouchButton

随手点个喜欢吧~

关注我

QQ--iOS 交流群:107548668

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,050评论 25 707
  • #下店六年级上课记录 2017/5/23# 绘本《The Princess Twins and the Tea P...
    萤火虫ABC阅读 532评论 0 0
  • 明天八月初七,不是中秋节,不是国庆节,也不是过年,而是老爸的生日。老爸今年已经六十七岁,可是做为女儿的我却...
    dym1979阅读 467评论 0 5