技术文章分类(180)

技术随笔(11)

ios悬浮按钮的实现

我在网上找了很久没有找到一个悬浮按钮,所以才决定自己做一个,效果还不错。

github 下载demo:https://github.com/MartinLi841538513/MartinDemos (一切以demo为准)

 

集成大法:

一,首先引入MLFloatButton.h .m .xib 文件(在demo含)

二,在controller中集成成MLFloatButton

引入#import "MLFloatButton.h"
实现MLFloatButtonDelegate协议以及他的方法buttonTouchAction
然后通过loadFromNibWithFrame初始化floatButton(这里一定要在viewWillAppear实现,否则,popBack将会看不到效果)
prepareForSegue是跳转到另外一个界面也要能够显示该floatButton(通常我们需要这么做),此时我们需要重新设置代理和父视图。并在下一个controller的viewWillAppear实现addSubview,以及协议方法。
#import "FloatButtonViewController.h"
#import "MLFloatButton.h"
#import "SecondViewController.h"
@interface FloatButtonViewController ()<MLFloatButtonDelegate>
{
    MLFloatButton *floatButton;
}
@end
@implementation FloatButtonViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    floatButton = [MLFloatButton loadFromNibWithFrame:CGRectMake(100, 100, 32, 32) addTarget:self InSuperView:self.view];//这里一定要在viewWillAppear实现,否则,popBack将会看不到效果
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"FloatButtonPush"]) {
        SecondViewController *target = segue.destinationViewController;
        target.floatButton = floatButton;
        target.floatButton.floatButtonDelegate = (id)target; //重新设置代理controller
    }
}

#pragma MLFloatButtonDelegate
-(void)buttonTouchAction{
    NSLog(@"button action");
}
@end

 

在SecondViewController.h

#import "MLFloatButton.h"
@interface SecondViewController : UIViewController
@property(nonatomic,strong)MLFloatButton *floatButton;
@end

 

在SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()<MLFloatButtonDelegate>
@end
@implementation SecondViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.floatButton showInFatherView:self.view];
}

#pragma MLFloatButtonDelegate
-(void)buttonTouchAction{
    NSLog(@"SecondViewController floatButton touched");
}
@end

 

posted @ 2014-11-25 20:14  坤哥MartinLi  阅读(2680)  评论(0编辑  收藏  举报