-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShapeView.m
79 lines (62 loc) · 2.33 KB
/
ShapeView.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
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// ShapeView.m
// GesturesDemo
//
#import "ShapeView.h"
@implementation ShapeView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
// add pan UIGestureRecognizer
UIPanGestureRecognizer *panGestureRecognizer =
[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[self addGestureRecognizer:panGestureRecognizer];
// add double tap UIGestureRecognizer
UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(delete:)];
doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTapGestureRecognizer];
// add tap UIGestureRecognizer
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(front:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleTapGestureRecognizer];
}
return self;
}
// respond to hit on shape
-(void)move:(UIPanGestureRecognizer *)panRecognizer{
CGPoint translation = [panRecognizer translationInView:self];
CGPoint imageViewPosition = self.center;
imageViewPosition.x += translation.x;
imageViewPosition.y += translation.y;
self.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView:self];
[panRecognizer cancelsTouchesInView];
}
-(void)front:(UITapGestureRecognizer *)tapRecognizer
{
[UIView animateWithDuration:0.25 animations:^{
[self.superview bringSubviewToFront:self];
self.transform = CGAffineTransformIdentity;
}];
[tapRecognizer cancelsTouchesInView];
}
-(void)delete:(UITapGestureRecognizer *)tapRecognizer
{
[UIView animateWithDuration:0.25 animations:^{
self.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)-960);
self.transform = CGAffineTransformIdentity;
[super removeFromSuperview];
}];
[tapRecognizer cancelsTouchesInView];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end