-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUILongPressMenuImageView.m
executable file
·77 lines (69 loc) · 2.8 KB
/
UILongPressMenuImageView.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
//
// UILongPressMenuImageView.m
// Coding_iOS
//
// Created by 王 原闯 on 14/10/25.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "UILongPressMenuImageView.h"
#import <objc/runtime.h>
@implementation UILongPressMenuImageView
#pragma mark LongCopyMenu
- (BOOL)canBecomeFirstResponder{
if (self.longPressMenuBlock) {
return YES;
}else{
return [super canBecomeFirstResponder];
}
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if (self.longPressMenuBlock) {
for (int i=0; i<self.longPressTitles.count; i++) {
if (action == NSSelectorFromString([NSString stringWithFormat:@"easeLongPressMenuClicked_%d:", i])) {
return YES;
}
}
return NO;
}else{
return [super canPerformAction:action withSender:sender];
}
}
- (void)addLongPressMenu:(NSArray *)titles clickBlock:(void(^)(NSInteger index, NSString *title))block{
self.longPressMenuBlock = block;
self.longPressTitles = titles;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self addGestureRecognizer:longPress];
}
-(void)handleLongPress:(UIGestureRecognizer*)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan) {
[self becomeFirstResponder];
NSMutableArray *menuItems = [[NSMutableArray alloc] initWithCapacity:self.longPressTitles.count];
Class cls = [self class];
SEL imp = @selector(longPressMenuClicked:);
for (int i=0; i<self.longPressTitles.count; i++) {
NSString *title = [self.longPressTitles objectAtIndex:i];
// 注册名添加方法sel,sel的具体实现在imp(longPressMenuClicked:)
SEL sel = sel_registerName([[NSString stringWithFormat:@"easeLongPressMenuClicked_%d:", i] UTF8String]);
class_addMethod(cls, sel, [cls instanceMethodForSelector:imp], "v@");
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:title action:sel];
[menuItems addObject:menuItem];
}
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:menuItems];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
}
- (void)longPressMenuClicked:(id)sender {
NSString *selStr = NSStringFromSelector(_cmd);
NSString *preFix = @"easeLongPressMenuClicked_";
NSString *indexStr = [selStr substringFromIndex:preFix.length];
NSInteger index = indexStr.integerValue;
if (index >=0 && index<self.longPressTitles.count) {
NSString *title = [self.longPressTitles objectAtIndex:index];
if (self.longPressMenuBlock) {
self.longPressMenuBlock(index, title);
}
}
}
@end