Created
November 12, 2016 05:19
-
-
Save shamatar/adaaa475557b4e2979bf48b63238aed7 to your computer and use it in GitHub Desktop.
Drop in replacement of UIImageView that supports copy on long-press interaction. In principle - easily editable to arbitrary interaction as it uses block and protocol for functionality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FSCopyableImage.h | |
// | |
// Created by Alexander Vlasov on 11.11.16. | |
// Copyright © 2016 Alexander Vlasov. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@protocol FSCopyableImageViewDelegate; | |
@interface FSCopyableImageView : UIImageView | |
@property (nonatomic, unsafe_unretained) id<FSCopyableImageViewDelegate> delegate; | |
@property (nonatomic, copy) void (^imageCopied)(UIImage *copiedImage); | |
@property (nonatomic, assign, setter = setCopyingEnabled:, getter = isCopyingEnabled) BOOL copyingEnabled; | |
@property (nonatomic, assign, setter = setMinimumPressDuration:) CFTimeInterval minimumPressDuration; | |
- (instancetype)initWithFrame:(CGRect)frame delegate:(id<FSCopyableImageViewDelegate>)delegate; | |
- (instancetype)initWithFrame:(CGRect)frame copiedBlock:(void (^)(UIImage *copiedImage))copiedBlock; | |
@end | |
@protocol FSCopyableImageViewDelegate <NSObject> | |
@optional | |
- (void)image:(FSCopyableImageView *)copyableImage didCopyImage:(UIImage *)copiedImage; | |
@end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FSCopyableImage.m | |
// | |
// Created by Alexander Vlasov on 11.11.16. | |
// Copyright © 2016 Alexander Vlasov. All rights reserved. | |
// | |
#import "FSCopyableImageView.h" | |
@interface FSCopyableImageView () | |
@property (nonatomic, strong) UILongPressGestureRecognizer* longPressGestureRecognizer; | |
- (void)onLongGesture:(UILongPressGestureRecognizer *)gestureRecognizer; | |
@end | |
@implementation FSCopyableImageView | |
- (instancetype)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self setup]; | |
} | |
return self; | |
} | |
- (instancetype)initWithFrame:(CGRect)frame delegate:(id<FSCopyableImageViewDelegate>)delegate | |
{ | |
self = [self initWithFrame:frame]; | |
[self setDelegate:delegate]; | |
return self; | |
} | |
- (instancetype)initWithFrame:(CGRect)frame copiedBlock:(void (^)(UIImage *copiedImage))copiedBlock | |
{ | |
self = [self initWithFrame:frame]; | |
[self setImageCopied:copiedBlock]; | |
return self; | |
} | |
- (void)awakeFromNib | |
{ | |
[super awakeFromNib]; | |
[self setup]; | |
} | |
#pragma mark - Internal Methods | |
- (void)setup | |
{ | |
[self setCopyingEnabled:YES]; | |
self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] | |
initWithTarget:self action:@selector(onLongGesture:)]; | |
[self addGestureRecognizer:self.longPressGestureRecognizer]; | |
} | |
-(void)save{ | |
if (self.image!=nil){ | |
UIImageWriteToSavedPhotosAlbum(self.image,nil,nil,nil); | |
} | |
} | |
- (void)onLongGesture:(UILongPressGestureRecognizer *)gestureRecognizer | |
{ | |
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { | |
[self becomeFirstResponder]; | |
UIMenuController *menuController = [UIMenuController sharedMenuController]; | |
UIMenuItem *save = [[UIMenuItem alloc] initWithTitle:@"Сохранить" action:@selector(save)]; | |
menuController.menuItems = @[save]; | |
[menuController setTargetRect:self.bounds inView:self]; | |
[menuController setMenuVisible:YES animated:YES]; | |
} | |
} | |
#pragma mark - Custom Setters | |
- (void)setCopyingEnabled:(BOOL)enableCopying | |
{ | |
_copyingEnabled = enableCopying; | |
[self setUserInteractionEnabled:enableCopying]; | |
} | |
- (void)setMinimumPressDuration:(CFTimeInterval)minimumPressDuration | |
{ | |
_minimumPressDuration = minimumPressDuration; | |
[self.longPressGestureRecognizer setMinimumPressDuration:minimumPressDuration]; | |
} | |
#pragma mark - UIResponder | |
- (BOOL)canBecomeFirstResponder | |
{ | |
return self.copyingEnabled; | |
} | |
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender | |
{ | |
if (action == @selector(copy:)) { | |
return YES; | |
} | |
else { | |
return [super canPerformAction:action withSender:sender]; | |
} | |
} | |
- (void)copy:(id)sender | |
{ | |
UIImage *copiedImage = self.image; | |
if ([copiedImage isKindOfClass:[UIImage class]]) { | |
[[UIPasteboard generalPasteboard] setImage:copiedImage]; | |
} | |
if ([self.delegate respondsToSelector:@selector(image:didCopyImage:)]) { | |
[self.delegate image:self didCopyImage:self.image]; | |
} | |
if (self.imageCopied != nil) { | |
self.imageCopied(copiedImage); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment