forked from b051/RSMenuView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VHMenuFoldButton.m
64 lines (56 loc) · 1.95 KB
/
VHMenuFoldButton.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
//
// VHMenuFoldButton.m
//
// Created by Rex Sheng on 10/17/12.
// Copyright (c) 2012 Log(n) LLC. All rights reserved.
//
#import "VHMenuFoldButton.h"
#import "VHMenuRightView.h"
NSString * const kVHMenuOpening = @"opening";
NSString * const VHMenuOpenNotification = @"VHMenuOpenNotification";
@implementation VHMenuFoldButton
{
NSString *identifier;
BOOL opening;
UIButton *button;
id observer;
}
- (id)initWithIdentifier:(NSString *)_identifier attributes:(NSDictionary *)attributes
{
if (self = [super initWithFrame:CGRectZero]) {
identifier = _identifier;
button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *icon = [UIImage imageNamed:@"menu_arrow"];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
opening = [attributes[kVHMenuOpening] boolValue];
observer = [[NSNotificationCenter defaultCenter] addObserverForName:VHMenuOpenNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
if ([note.userInfo[kVHMenuIdentifier] isEqualToString:identifier]) {
BOOL status = [note.userInfo[kVHMenuOpening] boolValue];
[UIView animateWithDuration:.3 animations:^{
button.transform = CGAffineTransformMakeRotation(status ? 0 : -M_PI_2);
}];
}
}];
CGFloat r = MAX(icon.size.width, icon.size.height);
[button setImage:icon forState:UIControlStateNormal];
self.frame = button.frame = CGRectMake(0, 0, r, r);
button.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self addSubview:button];
if (!opening) {
button.transform = CGAffineTransformMakeRotation(-M_PI_2);
}
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
- (void)buttonClicked:(id)sender
{
opening = !opening;
if (identifier) {
[[NSNotificationCenter defaultCenter] postNotificationName:VHMenuOpenNotification object:self userInfo:@{kVHMenuOpening: @(opening), kVHMenuIdentifier: identifier}];
}
}
@end