iOS 可拖动的按钮

本文详细介绍了一个名为 SafeCenterButton 的 iOS 控件实现,该控件继承自 UIView,用于显示安全中心按钮,并处理触摸事件,包括开始、结束和移动,以实现按钮的交互功能。文章还介绍了如何初始化和使用 SafeCenterButton,以及其代理方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <UIKit/UIKit.h>

@protocol SafeCenterButtonDelegate <NSObject>

- (void)safeCenterButtonDidSelect;

@end

@interface SafeCenterButton : UIView

@property (nonatomic, weak) id<SafeCenterButtonDelegate> delegate;

- (instancetype)initWithImage:(NSString *)name top:(CGFloat)top bottom:(CGFloat)bottom frame:(CGRect)frame;

@end
#import "SafeCenterButton.h"

@interface SafeCenterButton ()
{
    //是否移动了,移动了取消点击事件
    CGFloat moveStepX;
    CGFloat moveStepY;
    CGFloat minTop;
    CGFloat maxTop;
}
@end

@implementation SafeCenterButton

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return NO;
}

- (instancetype)init {
    CGFloat bottom = 100 + 38;
    if (IS_iPhoneX) {
        bottom += 20;
    }
    return [self initWithImage:@"safecenter_main" top:NavigationBarHeight bottom:bottom frame:CGRectMake(0, ScreenHeight - bottom, 110, 38)];
}
- (instancetype)initWithImage:(NSString *)name top:(CGFloat)top bottom:(CGFloat)bottom frame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        minTop = top;
        maxTop = ScreenHeight - bottom;
        self.frame = frame;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
        imageView.frame = self.bounds;
        [self addSubview:imageView];
    }
    return self;
}

- (void)presentToSafeCenter {
    if ([self.delegate respondsToSelector:@selector(safeCenterButtonDidSelect)]) {
        [self.delegate safeCenterButtonDidSelect];
    }
}

- (void)moveToDefaultPosition {
    CGRect frame = self.frame;
    if (frame.origin.x <= (ScreenWidth - frame.size.width) / 2) {
        frame.origin.x = 0;
    } else {
        frame.origin.x = ScreenWidth - frame.size.width;
    }
    if (frame.origin.y < minTop) {
        frame.origin.y = minTop;
    } else if (frame.origin.y > maxTop) {
        frame.origin.y = maxTop;
    }
    [UIView animateWithDuration:TimeInterval_Animation animations:^{
        self.frame = frame;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    moveStepX = 0;
    moveStepY = 0;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    if (fabs(moveStepX) < 1 && fabs(moveStepY) < 1) {
        [self presentToSafeCenter];
    }
    __weak typeof(self) weakSelf = self;
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        [weakSelf moveToDefaultPosition];
    });
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self];
    if (@available(iOS 9.1, *)) {
        CGPoint previousPoint = [touch precisePreviousLocationInView:self];
        CGFloat offsetX = currentPoint.x - previousPoint.x;
        CGFloat offsetY = currentPoint.y - previousPoint.y;
//        NSLog(@"%f  %f", offsetX, offsetY);
        moveStepX += offsetX;
        moveStepY += offsetY;
        self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
    } else {
        
    }
}

@end

使用

eCenterButton *safeCenterButton = [[SafeCenterButton alloc] initWithImage:@"safecenter_order" top:StatusBarHeight bottom:BottomHeight + 50 + 38 frame:CGRectMake(0, ScreenHeight - 110 - 38 - 20 - BottomHeight, 110, 38)];
    safeCenterButton.delegate = self;
    [self.view addSubview:safeCenterButton];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值