HKFloatBall.m
2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// HKFloatBall.m
// WeChatFloat
//
// Created by HeiKki on 2018/6/4.
// Copyright © 2018年 HeiKki. All rights reserved.
//
#import "HKFloatBall.h"
#import <CNLiveBaseKit/CNLiveDefinesHeader.h>
#define margin 10
@interface HKFloatBall()
@end
@implementation HKFloatBall
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.iconImageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[self addGestureRecognizer:tap];
}
return self;
}
-(UIImageView *)iconImageView{
if (!_iconImageView) {
_iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
_iconImageView.backgroundColor = [UIColor yellowColor];
// _iconImageView.image = [UIImageMake([[[[NSBundle mainBundle] infoDictionary] valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject]) imageByResizeToSize:CGSizeMake(self.frame.size.width, self.frame.size.height)];
// _iconImageView.image = [UIImage imageNamed:@"icon"];
_iconImageView.layer.borderWidth = 0;
_iconImageView.layer.borderColor = [UIColor grayColor].CGColor;
_iconImageView.layer.cornerRadius = self.bounds.size.width/2;
_iconImageView.layer.masksToBounds = YES;
};
return _iconImageView;
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.center = [touches.anyObject locationInView:[UIApplication sharedApplication].keyWindow];
if ([self.delegate respondsToSelector:@selector(floatBallBeginMove:)]) {
[self.delegate floatBallBeginMove:self];
}
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self endTouch:[touches.anyObject locationInView:[UIApplication sharedApplication].keyWindow]];
if ([self.delegate respondsToSelector:@selector(floatBallEndMove:)]) {
[self.delegate floatBallEndMove:self];
}
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self endTouch:[touches.anyObject locationInView:[UIApplication sharedApplication].keyWindow]];
if ([self.delegate respondsToSelector:@selector(floatBallEndMove:)]) {
[self.delegate floatBallEndMove:self];
}
}
- (void)endTouch:(CGPoint)point{
CGRect frame = self.frame;
if (point.x > KScreenWidth / 2) {
frame.origin.x = KScreenWidth - frame.size.width -margin;
}else{
frame.origin.x = margin;
}
if (frame.origin.y > KScreenHeight - 64) {
frame.origin.y = KScreenHeight -64;
}else if (frame.origin.y < 20) {
frame.origin.y = 20;
}
[UIView animateWithDuration:0.3 animations:^{
self.frame = frame;
}];
}
- (void)tap:(UIGestureRecognizer *)tap{
if ([self.delegate respondsToSelector:@selector(floatBallDidClick:)]) {
[self.delegate floatBallDidClick:self];
}
}
@end