Skip to content

Instantly share code, notes, and snippets.

@zydeco
Created May 17, 2016 20:28
Show Gist options
  • Save zydeco/144c2d0c8dc07ff1ce9be820819fc117 to your computer and use it in GitHub Desktop.
Save zydeco/144c2d0c8dc07ff1ce9be820819fc117 to your computer and use it in GitHub Desktop.
single file drop-in version of eumlab/EUMTouchPointView
/*
EUMShowTouchDropIn
single-file drop-in version
Copyright (c) 2014 Shawn Xiao <[email protected]>
Copyright (c) 2016 Jesús A. Álvarez <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#define kAnimationDuration 0.1
#define kStartScale 2
#define kEndScale 1.2
@interface EUMShowTouchWindow : UIWindow
@property (nonatomic, strong) UIImage *imgViewPointer;
@property (nonatomic, assign) CGSize pointerSize;
@property (nonatomic, strong) UIColor *pointerColor;
@property (nonatomic, strong) UIColor *pointerstrokeColor;
@end
@interface EUMTouchPointView : UIView
@property (nonatomic, strong) UIColor *pointerColor;
@property (nonatomic, strong) UIColor *pointerstrokeColor;
@end
@interface UITouch (EUMUITouch)
@property (nonatomic, strong) UIView *viewTouchPointer;
@end
id customWindowImp(id self, SEL _cmd) {
static id customWindow = nil;
if (customWindow == nil) {
customWindow = [[EUMShowTouchWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
}
return customWindow;
}
static void (*originalSetDelegateImp)(id, SEL, id);
void customSetDelegate(id self, SEL _cmd, id delegate) {
originalSetDelegateImp(self, _cmd, delegate);
class_replaceMethod([delegate class], @selector(window),
(IMP)customWindowImp, "@@:");
}
@implementation EUMShowTouchWindow
+ (void)load {
// inject into app delegate
Method originalSetDelegate =
class_getInstanceMethod([UIApplication class], @selector(setDelegate:));
originalSetDelegateImp =
(void (*)(id, SEL, id))method_getImplementation(originalSetDelegate);
class_replaceMethod([UIApplication class], @selector(setDelegate:),
(IMP)customSetDelegate, "v@:@");
}
- (instancetype)init {
self = [super init];
if (self) {
self.pointerSize = CGSizeMake(50, 50);
}
return self;
}
- (CGSize)pointerSize {
if (_pointerSize.height <= 10 || _pointerSize.width <= 10) {
_pointerSize = CGSizeMake(50, 50);
}
return _pointerSize;
}
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
if (event.type == UIEventTypeTouches) {
for (UITouch *touch in [event allTouches]) {
if (touch.phase == UITouchPhaseBegan) {
CGPoint point = [touch locationInView:self];
EUMTouchPointView *touchPointerView = [[EUMTouchPointView alloc]
initWithFrame:CGRectMake(
point.x - self.pointerSize.width / 2,
point.y - self.pointerSize.height / 2,
self.pointerSize.width,
self.pointerSize.height)];
touchPointerView.contentMode = UIViewContentModeRedraw;
touchPointerView.pointerColor = self.pointerColor;
touchPointerView.pointerstrokeColor = self.pointerstrokeColor;
[self addSubview:touchPointerView];
touch.viewTouchPointer = touchPointerView;
touchPointerView.transform =
CGAffineTransformMakeScale(kStartScale, kStartScale);
touchPointerView.alpha = 0;
[UIView animateWithDuration:kAnimationDuration
animations:^{
touchPointerView.transform =
CGAffineTransformIdentity;
touchPointerView.alpha = 1;
}];
} else if (touch.phase == UITouchPhaseCancelled ||
touch.phase == UITouchPhaseEnded) {
[UIView animateWithDuration:kAnimationDuration
animations:^{
touch.viewTouchPointer.transform =
CGAffineTransformMakeScale(kEndScale, kEndScale);
touch.viewTouchPointer.alpha = 0;
}
completion:^(BOOL finished) {
[touch.viewTouchPointer removeFromSuperview];
}];
} else if (touch.phase == UITouchPhaseMoved) {
CGPoint point = [touch locationInView:self];
CGRect tFrame = touch.viewTouchPointer.frame;
tFrame.origin.x = point.x - self.pointerSize.width / 2;
tFrame.origin.y = point.y - self.pointerSize.height / 2;
touch.viewTouchPointer.frame = tFrame;
}
}
}
}
@end
@implementation EUMTouchPointView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGRect frm = self.frame;
[self drawPointer:frm.size stockWidth:3];
}
- (void)drawPointer:(CGSize)pointSize stockWidth:(CGFloat)stockWidth {
//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor *color = [UIColor colorWithRed:0 green:0.734 blue:1 alpha:0.694];
if (self.pointerColor) {
color = self.pointerColor;
}
//// Shadow Declarations
UIColor *shadow = [UIColor.blackColor colorWithAlphaComponent:0.2];
CGSize shadowOffset = CGSizeMake(0.1, -0.1);
CGFloat shadowBlurRadius = 4;
//// Oval Drawing
UIBezierPath *ovalPath = [UIBezierPath
bezierPathWithOvalInRect:CGRectMake(5, 5, pointSize.width - 10,
pointSize.height - 10)];
[color setFill];
[ovalPath fill];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius,
[shadow CGColor]);
if (self.pointerstrokeColor) {
[self.pointerstrokeColor setStroke];
} else {
[UIColor.whiteColor setStroke];
}
ovalPath.lineWidth = stockWidth;
[ovalPath stroke];
CGContextRestoreGState(context);
}
@end
static void *keyPointerView;
@implementation UITouch (EUMUITouch)
- (UIView *)viewTouchPointer {
return objc_getAssociatedObject(self, keyPointerView);
}
- (void)setViewTouchPointer:(UIView *)__viewTouchPointer {
objc_setAssociatedObject(self, keyPointerView, __viewTouchPointer,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
@zydeco
Copy link
Author

zydeco commented May 17, 2016

Single file version of eumlab/EUMTouchPointView

Just drop it in your project and compile it when you want to show touches, no modification of the app delegate needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment