#import "UICopyImageView.h"
#import "SVProgressHUD.h"
@interface UICopyImageView ()<UIAlertViewDelegate>
@property (nonatomic, strong, readonly) UILongPressGestureRecognizer *longPressGestureRecognizer;
@property (nonatomic, assign) UIMenuControllerArrowDirection copyMenuArrowDirection;
@property (nonatomic, assign) BOOL copyingEnabled; // Defaults to YES
@end
@implementation UICopyImageView
//UILabel默认是不接收事件的,我们需要自己添加touch事件
-(void)attachTapHandler
{
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:_longPressGestureRecognizer];
_longPressGestureRecognizer.minimumPressDuration = 0.5;
[self addGestureRecognizer:_longPressGestureRecognizer];
_copyingEnabled = YES;
}
//绑定事件
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self attachTapHandler];
}
return self;
}
//同上
-(void)awakeFromNib
{
[super awakeFromNib];
[self attachTapHandler];
}
-(void)handleTap:(UILongPressGestureRecognizer*) recognizer
{
//[self becomeFirstResponder];
if (recognizer.state==UIGestureRecognizerStateBegan) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"保存相册" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
break;
default:
break;
}
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
// Was there an error?
if (error != NULL)
{
// Show error message...
[SVProgressHUD showImage:nil status:@"保存失败"];
}
else // No errors
{
[SVProgressHUD showImage:nil status:@"保存成功"];
// Show message image successfully saved
}
}