- 自定義一個Widget,例如
XXLabel
- 實現
– (BOOL)canBecomeFirstResponder
, 且返回YES
- 實現
– (BOOL)canPerformAction:withSender
, 并根據需求返回YES
或NO
(貌似可以不用實現,簡書不顯示過期文字標簽)
代碼如下
#import "XXLabel.h"
@implementation XXLabel
- (void)awakeFromNib
{
[self addLongPressGesture];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
#pragma mark - Private Method
- (void)addLongPressGesture
{
self.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToCollection:)];
[self addGestureRecognizer:longPressGR];
}
- (void)longPressToCollection:(UIGestureRecognizer *)recognizer
{
[self becomeFirstResponder];
UIMenuItem *collection = [[UIMenuItem alloc] initWithTitle:@"收藏" action:@selector(collectionAction:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:collection, nil]];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
- (void)collectionAction:(id)sender
{
// 使用Block或者代理
NSLog(@"點擊了收藏");
}
@end
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。