Skip to content

Instantly share code, notes, and snippets.

@brunophilipe
Last active May 13, 2020 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunophilipe/00d9940b5417b77b770e39f67b6b4d45 to your computer and use it in GitHub Desktop.
Save brunophilipe/00d9940b5417b77b770e39f67b6b4d45 to your computer and use it in GitHub Desktop.
UIButton.pointerShapeProvider workaround
//
// BPPointerShapeHelper.h
//
// Created by Bruno Philipe on 12.05.20.
// Copyright © 2020 Bruno Philipe. All rights reserved.
//
// 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 <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface BPPointerShapeHelper: NSObject<UIPointerInteractionDelegate>
+ (void)setRoundedRectPointerShapeProviderForView:(UIView * _Nonnull)view
usingProvider:(void (^)(CGRect * _Nonnull, CGFloat * _Nonnull))provider;
+ (void)setBezierPathPointerShapeProviderForView:(UIView * _Nonnull)view
usingProvider:(void (^)(UIBezierPath * _Nullable * _Nullable))provider;
@end
NS_ASSUME_NONNULL_END
//
// BPPointerShapeHelper.m
//
// Created by Bruno Philipe on 12.05.20.
// Copyright © 2020 Bruno Philipe. All rights reserved.
//
// 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 "BPPointerShapeHelper.h"
#import <objc/runtime.h>
typedef NS_ENUM(NSUInteger, BPPointerShapeOption) {
BPPointerShapeRoundedRect, BPPointerShapeBezierPath
};
@interface BPPointerShapeHelper ()
@property (nonatomic) BPPointerShapeOption pointerShapeOption;
@property (nonatomic, copy, nullable) void (^roundedRectPointerShapeProvider)(CGRect * _Nonnull, CGFloat * _Nonnull);
@property (nonatomic, copy, nullable) void (^bezierPathPointerShapeProvider)(UIBezierPath **);
@end
@implementation BPPointerShapeHelper
+ (void)setRoundedRectPointerShapeProviderForView:(UIView * _Nonnull)view
usingProvider:(void (^)(CGRect * _Nonnull, CGFloat * _Nonnull))provider;
{
BPPointerShapeHelper *helper = [[BPPointerShapeHelper alloc] init];
[helper setRoundedRectPointerShapeProvider:provider];
[helper setPointerShapeOption:BPPointerShapeRoundedRect];
[view addInteraction:[[UIPointerInteraction alloc] initWithDelegate:helper]];
objc_setAssociatedObject(view, "BPPointerShapeHelper", helper, OBJC_ASSOCIATION_RETAIN);
}
+ (void)setBezierPathPointerShapeProviderForView:(UIView * _Nonnull)view
usingProvider:(void (^)(UIBezierPath **))provider;
{
BPPointerShapeHelper *helper = [[BPPointerShapeHelper alloc] init];
[helper setBezierPathPointerShapeProvider:provider];
[helper setPointerShapeOption:BPPointerShapeBezierPath];
[view addInteraction:[[UIPointerInteraction alloc] initWithDelegate:helper]];
objc_setAssociatedObject(view, "BPPointerShapeHelper", helper, OBJC_ASSOCIATION_RETAIN);
}
- (UIPointerStyle *)pointerInteraction:(UIPointerInteraction *)interaction styleForRegion:(UIPointerRegion *)region
{
switch (_pointerShapeOption) {
case BPPointerShapeRoundedRect: {
if (_roundedRectPointerShapeProvider != nil) {
CGRect rect = CGRectZero;
CGFloat radius = 0;
_roundedRectPointerShapeProvider(&rect, &radius);
return [UIPointerStyle styleWithShape:[UIPointerShape shapeWithRoundedRect:rect cornerRadius:radius]
constrainedAxes:UIAxisNeither];
}
break;
}
case BPPointerShapeBezierPath: {
if (_bezierPathPointerShapeProvider != nil) {
UIBezierPath *path = nil;
_bezierPathPointerShapeProvider(&path);
if (path == nil) { return nil; }
return [UIPointerStyle styleWithShape:[UIPointerShape shapeWithPath:path]
constrainedAxes:UIAxisNeither];
}
}
}
return nil;
}
@end
BPPointerShapeHelper.setBezierPathPointerShapeProviderFor(self) { [weak self] bezierPathPointer in
guard let self = self else { return }
bezierPathPointer?.pointee = UIBezierPath(rect: self.bounds)
}
BPPointerShapeHelper.setRoundedRectPointerShapeProviderFor(self) { [weak self] (rect, radius) in
guard let self = self else { return }
rect.pointee = self.bounds
radius.pointee = 6.0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment