iOS悬浮异形按钮实现

作者: 逐步腾飞 | 来源:发表于2016-11-30 15:51 被阅读274次

我们在开发中有时候根据业务的需要,有时候需要一个在界面上常驻按钮,或者悬浮按钮,且可拖动,可点击,该类按钮主要起到对公司业务进行推广或者突显新功能的作用,今天我们就来实现下,先看下效果图:

demo.gif

代码:

DysmorphismButton按钮在VC中的使用:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *bgImage = [UIImage imageNamed:@"imageButton"];
    DysmorphismButton *button = [[DysmorphismButton alloc] init];
    button.MoveEnable = YES;
    [button setBackgroundImage:bgImage forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"imageData"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(fileButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    button.frame =
    CGRectMake(self.view.frame.size.width - dyButtonWidth - dyButtonMargin, dyButtonMargin + topNavInsetHeight, dyButtonWidth , dyButtonHeight);
    button.topMargin = dyButtonMargin;
    button.layer.cornerRadius = dyButtonWidth / 2.0f;
    button.clipsToBounds = YES;
    self.dyButton = button;
    [self.view addSubview:self.dyButton];
}

//button action
- (void)fileButtonClicked{
    //TODO
}

DysmorphismButton 按钮实现部分:

.h文件
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface DysmorphismButton : UIButton {
    BOOL MoveEnable;
    BOOL MoveEnabled;
    CGPoint benginpoint;
}

@property(nonatomic, assign) BOOL MoveEnable;
@property(nonatomic, assign) BOOL MoveEnabled;
@property(nonatomic, assign) CGFloat topMargin;

@end
.m文件

#define kButtonXoffset 7
#define topNavInsetHeight 64
#import "DysmorphismButton.h"
#import "UIViewAdditions.h"

@implementation DysmorphismButton
@synthesize MoveEnable;
@synthesize MoveEnabled;

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

//touchBegan
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    MoveEnabled = NO;
    [super touchesBegan:touches withEvent:event];
    if (!MoveEnable) {
        return;
    }
    UITouch *touch = [touches anyObject];
    benginpoint = [touch locationInView:self];
}

//move

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (!MoveEnable) {
        return;
    }
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self];
    float offsetX = currentPosition.x - benginpoint.x;
    float offsetY = currentPosition.y - benginpoint.y;
    if (ABS(offsetX) < 4 && ABS(offsetY) < 4) {
        return;
    }
    MoveEnabled = YES;
    //移动后坐标
    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 - kButtonXoffset)) {
        CGFloat x = self.superview.frame.size.width - self.frame.size.width / 2 - kButtonXoffset;
        self.center = CGPointMake(x, self.center.y);
    } else if (self.center.x <= (self.frame.size.width / 2 + kButtonXoffset)) {
        CGFloat x = self.frame.size.width / 2 + kButtonXoffset;
        self.center = CGPointMake(x, self.center.y);
    }
    
    // y轴上下极限坐标
    CGFloat yOffset = topNavInsetHeight + kButtonXoffset + self.topMargin;
    if (self.center.y > (self.superview.frame.size.height - self.frame.size.height / 2 - kButtonXoffset)) {
        CGFloat x = self.center.x;
        CGFloat y = self.superview.frame.size.height - self.frame.size.height / 2 - kButtonXoffset;
        self.center = CGPointMake(x, y);
    } else if (self.center.y <= self.frame.size.height / 2 + yOffset) {
        CGFloat x = self.center.x;
        CGFloat y = self.frame.size.height / 2;
        self.center = CGPointMake(x, y + yOffset);
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!MoveEnable) {
        return;
    }
    
    [self handleButtonMoveEndEvent];
    [super touchesEnded:touches withEvent:event];
}

- (void)handleButtonMoveEndEvent {
    if (self.center.x >= self.superview.frame.size.width / 2) {  //向右侧移动
        //偏移动画
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:.5];
        [UIView setAnimationDelegate:self];
        self.frame = CGRectMake(self.superview.width - self.width - kButtonXoffset, self.top, self.width, self.height);
        //提交UIView动画
        [UIView commitAnimations];
    } else {  //向左侧移动
        
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:.5];
        [UIView setAnimationDelegate:self];
        self.frame = CGRectMake(0.f + kButtonXoffset, self.top, self.width, self.height);
        //提交UIView动画
        [UIView commitAnimations];
    }
}
@end

Demo URL:
(链接: https://pan.baidu.com/s/1nv55Qid 密码: 79pi)

相关文章

  • iOS悬浮异形按钮实现

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

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

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

  • iOS 悬浮按钮的实现

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

  • iOS 吸附悬浮按钮实现

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

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

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

  • iOS悬浮按钮的swift实现

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

  • iOS 悬浮按钮

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

  • ios 悬浮按钮

    效果图 核心代码

  • iOS 悬浮按钮

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

  • iOS 悬浮按钮

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

网友评论

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

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