美文网首页
全局悬浮窗的实现

全局悬浮窗的实现

作者: Coder东 | 来源:发表于2019-05-08 11:28 被阅读0次

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

//
//  SuspensionImage.h
//  全局悬浮窗
//
//  Created by 随风流年 on 2019/4/5.
//  Copyright © 2019 随风流年. All rights reserved.
//

#import <UIKit/UIKit.h>

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<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
    shouldPassEvent = YES;
    [[self superview] bringSubviewToFront:self];
}

-(void)touchesMoved:(NSSet<UITouch *> *)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<UITouch *> *)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

相关文章

网友评论

      本文标题:全局悬浮窗的实现

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