iOS 悬浮按钮的实现

作者: 小兵快跑 | 来源:发表于2016-03-23 08:38 被阅读9340次

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

参考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

相关文章

  • iOS - 添加一个全局悬浮按钮

    iOS - 添加一个全局悬浮按钮 iOS - 添加一个全局悬浮按钮

  • iOS 悬浮按钮的实现

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

  • iOS悬浮异形按钮实现

    我们在开发中有时候根据业务的需要,有时候需要一个在界面上常驻按钮,或者悬浮按钮,且可拖动,可点击,该类按钮主要起到...

  • iOS 吸附悬浮按钮实现

    实现按钮吸附在屏幕左右两侧效果 效果图 代码

  • iOS悬浮按钮的swift实现

    效果图如下: 一个iOS上的悬浮按钮,实现方式是写一个自定义类继承自UIButton ,重写3个方法: touch...

  • 【iOS开发】一个简单的拖拽悬浮按钮的实现

    【iOS开发】一个简单的拖拽悬浮按钮的实现直接上图: 实现原理使用UIButton的点击事件和手势来实现 在UIV...

  • iOS 悬浮按钮

    新建继承于UIWindow的类.h文件如下 .m 实现文件 附:我的博客地址

  • ios 悬浮按钮

    效果图 核心代码

  • iOS 悬浮按钮

    想要做出悬浮在屏幕上的按钮效果,按钮可以随便移动位置, 其实很简单的,没有想象中的那么难, 其中有两种方法可以做出...

  • iOS 悬浮按钮

    #import "ViewController.h"@interface ViewController ()@pr...

网友评论

  • 吉s她Hmm:楼主,你这个demo能否支持在制定的页面显示,别的页面不显示啊,大神。求告知啊
    小兵快跑:@吉s她Hmm 你在改改代码,可以满足您拖动时 bug 的
    吉s她Hmm:@小兵快跑 嗯 我改了一下你的代码 可以了 谢谢了大神 就是现在遇见的问题是每次拖动 都会触发点击的方法
    小兵快跑:@吉s她Hmm 可以的
  • 8fd28ad15588:请问 大神 android应用下怎么实现这个功能
  • ae4d01a800dc:问下 在tableViewCOntroller怎么固定在一个位置呢
  • IT播客:楼主 发个demo吧 邮箱1144547311@qq.com
    小兵快跑:到群空间里面找下
  • 木子影:怎么解决拖动和点击冲突的问题啊!
  • 多多_:https://github.com/WangMasterpro/DriftButton_Global 发现一个这个demo很适合我的需求,楼主多谢~
    雷鸣1010:下载下来后根本打不开呀
    小兵快跑:@多多_ :+1:
  • 文心楠_YouHou:能发下你的demo吗,957638713@qq.com
  • 晓蜻蜓:楼主有swift 版吗?
  • macfai:是啊,楼主,能发个demo 吗,多谢了
    macfai:@小兵快跑 好的,多谢群主:smile:
    小兵快跑:@macfai 在群文件里
  • 名扬丶四海:Demo链接能不能发一下
    小兵快跑:@iOS_Country 在群文件里
  • TongRy:哥们,你这个只能在本页面吧,跳转到其他页面还有吗?我最近也在研究,添加一个UIWindow,可是还是有点不如人意!
    TongRy:@小兵快跑 好的,马上加
    小兵快跑:@TongRy 加入QQ群107548668,我截图发给你。
    小兵快跑:@TongRy 有的,明天再发,困了,眼睛痛得快挣不开了;
  • 菜瓜弟弟:虽然你的想法很好,但是苹果审核肯定过不了:smile:
    小兵快跑:@阶梯 谢谢,大家一起交流交流
    菜瓜弟弟:@阶梯 因为苹果有规定,这种touch图标和ios系统自带的风格类似
    阶梯:@菜瓜弟弟 为什么呢?

本文标题:iOS 悬浮按钮的实现

本文链接:https://www.haomeiwen.com/subject/xzsllttx.html