实现一个可随手指拖动的View(OC版)

前言

项目中有类似微信视频通话最小化后,悬浮一个小窗口的功能,同时需要这个窗口可拖动。下面是窗口可拖动实现过程。

在这里插入图片描述

代码

新建FloatView继承自UIViewUIWindow,我这里的视频小窗口采用UIWindow实现,所以继承自UIWindow

//.h代码
@interface FloatWindow : UIWindow

@property (nonatomic, nullable, copy) void(^floatWindwDidClickBlock)(FloatWindow *window);

@end
//.m代码
static CGFloat kMargin = 10.f;

#define is_iPhoneXSerious @available(iOS 11.0, *) && UIApplication.sharedApplication.keyWindow.safeAreaInsets.bottom > 0.0

@implementation FloatWindow

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.center = [touches.anyObject locationInView:[UIApplication sharedApplication].delegate.window];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endTouch:[touches.anyObject locationInView:[UIApplication sharedApplication].delegate.window]];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endTouch:[touches.anyObject locationInView:[UIApplication sharedApplication].delegate.window]];
}

- (void)endTouch:(CGPoint)point {
    
    CGRect frame = self.frame;
    CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
    CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
    
    if (point.x > screenWidth / 2.f) {
        frame.origin.x = screenWidth - frame.size.width - kMargin;
    } else {
        frame.origin.x = kMargin;
    }
    CGFloat maxY = screenHeight - frame.size.height - (is_iPhoneXSerious ? 34:0);
    CGFloat minY = is_iPhoneXSerious ? 44:20;
    if (frame.origin.y > maxY) {
        frame.origin.y = maxY;
    } else if (frame.origin.y < minY) {
        frame.origin.y = minY;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = frame;
    }];
}

#pragma mark - tap gesture

- (void)tap:(UITapGestureRecognizer *)gesture {
    if (self.floatWindwDidClickBlock) {
        self.floatWindwDidClickBlock(self);
    }
}

注意点

如果只是单纯实现一个可拖动的视图的话,请继承UIView而不是UIWindow,然后调用initWithFrame方法初始化View,在View上可以根据项目添加自定义的控件元素,最后将视图addSubview到所需界面上即可。
如果要实现一个可拖动的UIWindow的话,可以继承UIWindow

另外,后续会在此基础上写一篇文章记录微信视频通话最小化后浮窗功能实现过程。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值