forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuButton.m
More file actions
executable file
·125 lines (105 loc) · 4.43 KB
/
MenuButton.m
File metadata and controls
executable file
·125 lines (105 loc) · 4.43 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// MenuButton.m
// JackFastKit
//
// Created by 曾 宪华 on 14-10-13.
// Copyright (c) 2014年 华捷 iOS软件开发工程师 曾宪华. All rights reserved.
//
#import "MenuButton.h"
#import <POP.h>
// Model
#import "MenuItem.h"
// View
#import "GlowImageView.h"
@interface MenuButton ()
@property (nonatomic, strong) GlowImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) MenuItem *menuItem;
@end
@implementation MenuButton
- (instancetype)initWithFrame:(CGRect)frame menuItem:(MenuItem *)menuItem {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.menuItem = menuItem;
CGSize imageSize = menuItem.iconImage.size;
if (!kDevice_Is_iPhone6Plus && !kDevice_Is_iPhone6) {
imageSize = CGSizeMake(imageSize.width *0.9, imageSize.height *0.9);
}
self.iconImageView = [[GlowImageView alloc] initWithFrame:CGRectMake(0, 0, imageSize.width, imageSize.height)];
self.iconImageView.userInteractionEnabled = NO;
[self.iconImageView setImage:menuItem.iconImage forState:UIControlStateNormal];
self.iconImageView.glowColor = menuItem.glowColor;
self.iconImageView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.iconImageView.bounds));
[self addSubview:self.iconImageView];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.iconImageView.frame) + 20, CGRectGetWidth(self.bounds), 20)];
self.titleLabel.textColor = kColorDark4;
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.font = [UIFont systemFontOfSize:15];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.text = menuItem.title;
CGPoint center = self.titleLabel.center;
center.x = CGRectGetMidX(self.bounds);
self.titleLabel.center = center;
[self addSubview:self.titleLabel];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self popBegan];
}
- (void)popBegan{
self.alpha = 0.7;
// [self pop_removeAllAnimations];
// // 播放缩放动画
// POPSpringAnimation *scaleAnimation = [POPSpringAnimation animation];
// scaleAnimation.springBounciness = 20; // value between 0-20
// scaleAnimation.springSpeed = 20; // value between 0-20
// scaleAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewScaleXY];
// scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.3, 1.3)];
// [self pop_addAnimation:scaleAnimation forKey:@"scaleAnimationKey"];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self disMissCompleted:NULL];
}
- (void)disMissCompleted:(void(^)(BOOL finished))completed {
self.alpha = 1.0;
if (completed) {
completed(YES);
}
// POPSpringAnimation *scaleAnimation = [POPSpringAnimation animation];
// scaleAnimation.springBounciness = 16; // value between 0-20
// scaleAnimation.springSpeed = 14; // value between 0-20
// scaleAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewScaleXY];
// scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.0, 1.0)];
// scaleAnimation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
// if (completed) {
// completed(finished);
// }
// };
// [self pop_addAnimation:scaleAnimation forKey:@"scaleAnimationKey"];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *curTouch = [touches anyObject];
if (CGRectContainsPoint(self.bounds, [curTouch locationInView:self]) &&
!CGRectContainsPoint(self.bounds, [curTouch previousLocationInView:self])) {
[self popBegan];
}else if (!CGRectContainsPoint(self.bounds, [curTouch locationInView:self]) &&
CGRectContainsPoint(self.bounds, [curTouch previousLocationInView:self])){
[self disMissCompleted:NULL];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *curTouch = [touches anyObject];
if (CGRectContainsPoint(self.bounds, [curTouch locationInView:self])) {
// 回调
[self disMissCompleted:^(BOOL finished) {
if (self.didSelctedItemCompleted) {
self.didSelctedItemCompleted(self.menuItem);
}
}];
}else{
[self disMissCompleted:NULL];
}
}
@end