forked from khanhduytran0/FrontBoardAppLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecoratedFloatingView.m
66 lines (53 loc) · 2.67 KB
/
DecoratedFloatingView.m
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
#import "DecoratedFloatingView.h"
@implementation DecoratedFloatingView
- (instancetype)initWithFrame:(CGRect)frame {
// Navigation bar
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 44)];
navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Unnamed window"];
navigationBar.items = @[navigationItem];
return [self initWithFrame:frame navigationBar:navigationBar];
}
- (instancetype)initWithFrame:(CGRect)frame navigationBar:(UINavigationBar *)navigationBar {
self = [super initWithFrame:frame];
self.layer.cornerRadius = 10;
self.layer.masksToBounds = YES;
self.navigationBar = navigationBar;
self.navigationItem = navigationBar.items.firstObject;
if (!self.navigationBar.superview) {
[self addSubview:self.navigationBar];
}
UIPanGestureRecognizer *moveGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveWindow:)];
moveGesture.minimumNumberOfTouches = 1;
moveGesture.maximumNumberOfTouches = 1;
[self.navigationBar addGestureRecognizer:moveGesture];
// Resize handle (idea stolen from Notes debugging window)
self.resizeHandle = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - 30, self.frame.size.height - 30, 60, 60)];
self.resizeHandle.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
self.resizeHandle.transform = CGAffineTransformMakeRotation(M_PI_4);
[self addSubview:self.resizeHandle];
UIPanGestureRecognizer *resizeGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(resizeWindow:)];
resizeGesture.minimumNumberOfTouches = 1;
resizeGesture.maximumNumberOfTouches = 1;
[self.resizeHandle addGestureRecognizer:resizeGesture];
return self;
}
- (void)moveWindow:(UIPanGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
[self.superview
bringSubviewToFront:self];
}
CGPoint point = [sender translationInView:self];
[sender setTranslation:CGPointZero inView:self];
self.center = CGPointMake(self.center.x + point.x, self.center.y + point.y);
}
- (void)resizeWindow:(UIPanGestureRecognizer*)sender {
CGPoint point = [sender translationInView:self];
[sender setTranslation:CGPointZero inView:self];
CGRect frame = self.frame;
frame.size.width = MAX(50, frame.size.width + point.x);
frame.size.height = MAX(50, frame.size.height + point.y);
self.frame = frame;
self.resizeHandle.center = CGPointMake(self.resizeHandle.center.x + point.x, self.resizeHandle.center.y + point.y);
}
@end