小程序全局悬浮窗_全局悬浮窗的实现

本文介绍了如何在小程序中实现全局悬浮窗,包括创建SuspensionImage类,设置触摸事件处理,确保视图在屏幕内移动,并提供点击事件的回调功能。通过SuspensionImage的实例方法getTapInfoWithMethod,实现了点击后执行指定的block。

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

最近项目想要展示一个悬浮窗,显示一些广告信息,所以就写了一个简单的样式,在此记录下

//

// SuspensionImage.h

// 全局悬浮窗

//

// Created by 随风流年 on 2019/4/5.

// Copyright © 2019 随风流年. All rights reserved.

//

#import

NS_ASSUME_NONNULL_BEGIN

typedef void (^tapImageBlcok) (void);

@interface SuspensionImage : UIImageView

//block作为属性使用 点击图片方法的block

@property (nonatomic, copy) void(^completeBlock)(void);

//block 作为参数

-(void)getTapInfoWithMethod:(tapImageBlcok)block;

@end

NS_ASSUME_NONNULL_END

具体的实现类

//

// SuspensionImage.m

// 全局悬浮窗

//

// Created by 随风流年 on 2019/4/5.

// Copyright © 2019 随风流年. All rights reserved.

//

#import "SuspensionImage.h"

#import "UIView+Addition.h"

@interface SuspensionImage()

{

CGPoint startLocation;

BOOL shouldPassEvent; // 是否需要传递事件

}

@end

@implementation SuspensionImage

-(instancetype)initWithFrame:(CGRect)frame{

self = [super initWithFrame:frame];

if (self) {

self.userInteractionEnabled = YES;

}

return self;

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

CGPoint pt = [[touches anyObject] locationInView:self];

startLocation = pt;

shouldPassEvent = YES;

[[self superview] bringSubviewToFront:self];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

CGPoint pt = [[touches anyObject]locationInView:self];

float dx = pt.x - startLocation.x;

float dy = pt.y - startLocation.y;

//计算移动后的view中心点

CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy);

/* 限制用户不可将视图拖出屏幕 */

float halfx = CGRectGetMidX(self.bounds);

//x坐标左边界

newCenter.x = MAX(halfx, newCenter.x);

//x坐标右边界

newCenter.x = MIN(self.superview.bounds.size.width - halfx, newCenter.x);

//y坐标同理

float halfy = CGRectGetMidY(self.bounds);

// y坐标上边界

newCenter.y = MAX(halfy, newCenter.y);

newCenter.y = MIN(self.superview.bounds.size.height - halfy, newCenter.y);

// 判断是否需要传递事件

shouldPassEvent = CGPointEqualToPoint(newCenter, startLocation);

//移动view

self.center = newCenter;

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

CGPoint point = self.center;

if (point.x > [self superview].width / 2.0) {

[UIView animateWithDuration:0.2 animations:^{

self.left = [self superview].width - self.width;

}];

}else{

[UIView animateWithDuration:0.2 animations:^{

self.left = 0;

}];

}

if (shouldPassEvent) {//点击事件 需要传递点击

[super touchesEnded:touches withEvent:event];

__weak typeof (self)weakSelf = self;

__strong typeof (weakSelf)strongSelf = weakSelf;

//block作为属性使用

if (strongSelf.completeBlock) {

strongSelf.completeBlock();

}

}

}

//对象方法中使用block

-(void)getTapInfoWithMethod:(tapImageBlcok)block{

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

block();//block作为参数使用

});

}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值