iOS悬浮异形按钮实现

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

iOS悬浮异形按钮实现_第1张图片
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 
#import 

@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 *)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 *)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悬浮异形按钮实现)