Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't crash on iOS because of an onClickBlock in react-native 0.73.2 #31

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ios/RNHoleView/RNHoleViewImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ typedef void(^AnimationFinishedCallback)();

@property (nonatomic, copy) AnimationFinishedCallback onAnimationFinishedFabric;

- (void)setOnClick:(void (^)(void))onClickBlock;

@end
93 changes: 53 additions & 40 deletions ios/RNHoleView/RNHoleViewImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ - (instancetype)initWitnX:(CGFloat)x y:(CGFloat)y
if (self) {
self.rect = CGRectMake(x, y, width, height);
self.borderRadius = borderRadius;

self.borderTopLeftRadius = borderTopLeftRadius == DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE ? borderRadius : borderTopLeftRadius;
self.borderTopRightRadius = borderTopRightRadius == DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE ? borderRadius : borderTopRightRadius;
self.borderBottomLeftRadius = borderBottomLeftRadius == DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE ? borderRadius : borderBottomLeftRadius;
Expand All @@ -50,6 +50,8 @@ @interface RNHoleViewImpl()<CAAnimationDelegate>

@property (nonatomic) dispatch_source_t holesTimer;

@property (nonatomic, copy) void (^onClickBlock)(void);

@end

@implementation RNHoleViewImpl
Expand All @@ -63,7 +65,7 @@ - (instancetype)init
_maskLayer.fillRule = kCAFillRuleEvenOdd;
_maskLayer.shouldRasterize = YES;
_maskLayer.rasterizationScale = [UIScreen mainScreen].scale;

self.layer.mask = _maskLayer;
}
return self;
Expand All @@ -73,27 +75,27 @@ - (instancetype)init
-(void)layoutSubviews
{
_maskLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

[self setMaskPath:self.holePaths skipAnimation:YES];
}


-(void)setHoles:(NSArray<NSDictionary *> *)holes
{
NSMutableArray <RNHoleViewHole*> *parsedHoles = @[].mutableCopy;

[holes enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
BOOL isRTL = obj[@"isRTL"] && [obj[@"isRTL"] boolValue] == YES;

CGFloat borderRadius = obj[@"borderRadius"] ? [obj[@"borderRadius"] floatValue] : DEFAULT_BORDER_RADIUS_VALUE;
CGFloat borderTopLeftRadius = obj[@"borderTopLeftRadius"] ? [obj[@"borderTopLeftRadius"] floatValue] : DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE;
CGFloat borderTopRightRadius = obj[@"borderTopRightRadius"] ? [obj[@"borderTopRightRadius"] floatValue] : DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE;
CGFloat borderBottomLeftRadius = obj[@"borderBottomLeftRadius"] ? [obj[@"borderBottomLeftRadius"] floatValue] : DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE;
CGFloat borderBottomRightRadius = obj[@"borderBottomRightRadius"] ? [obj[@"borderBottomRightRadius"] floatValue] : DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE;

if(obj[@"borderTopStartRadius"]){
CGFloat value = [obj[@"borderTopStartRadius"] floatValue];

if(value!=DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE){
if(isRTL){
borderTopRightRadius = value;
Expand All @@ -102,10 +104,10 @@ -(void)setHoles:(NSArray<NSDictionary *> *)holes
}
}
}

if(obj[@"borderTopEndRadius"]){
CGFloat value = [obj[@"borderTopEndRadius"] floatValue];

if(value!=DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE){
if(isRTL){
borderTopLeftRadius = value;
Expand All @@ -114,10 +116,10 @@ -(void)setHoles:(NSArray<NSDictionary *> *)holes
}
}
}

if(obj[@"borderBottomStartRadius"]){
CGFloat value = [obj[@"borderBottomStartRadius"] floatValue];

if(value!=DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE){
if(isRTL){
borderBottomRightRadius = value;
Expand All @@ -126,10 +128,10 @@ -(void)setHoles:(NSArray<NSDictionary *> *)holes
}
}
}

if(obj[@"borderBottomEndRadius"]){
CGFloat value = [obj[@"borderBottomEndRadius"] floatValue];

if(value!=DEFAULT_SPECIFIC_BORDER_RADIUS_VALUE){
if(isRTL){
borderBottomLeftRadius = value;
Expand All @@ -138,23 +140,23 @@ -(void)setHoles:(NSArray<NSDictionary *> *)holes
}
}
}

RNHoleViewHole *hole = [[RNHoleViewHole alloc] initWitnX:[obj[@"x"] floatValue] y:[obj[@"y"] floatValue] width:[obj[@"width"] floatValue] height:[obj[@"height"] floatValue] andBorderRadius:borderRadius andBorderTopLeftRadius:borderTopLeftRadius andBorderTopRightRadius:borderTopRightRadius andBorderBottomLeftRadius:borderBottomLeftRadius andBorderBottomRightRadius:borderBottomRightRadius];

[parsedHoles addObject:hole];
}];

self.parsedHoles = parsedHoles;
}


-(void)setAnimation:(NSDictionary *)animation{
_animation = animation;

if(_animation){
_animationDuration = animation[@"duration"];
NSString *timingFunction = animation[@"timingFunction"];

if([timingFunction isEqualToString:@"LINEAR"]){
_animationTimingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
}else if([timingFunction isEqualToString:@"EASE_IN"]){
Expand All @@ -164,28 +166,28 @@ -(void)setAnimation:(NSDictionary *)animation{
}else if([timingFunction isEqualToString:@"EASE_IN_OUT"]){
_animationTimingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
}

}
}


-(void)setParsedHoles:(NSArray<RNHoleViewHole *> *)parsedHoles
{
_parsedHoles = parsedHoles;

[self stopHolesTimer];

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());

if ( timer ) {
dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), 0.01 * NSEC_PER_SEC, (1ull * NSEC_PER_SEC) / 10);
dispatch_source_set_event_handler(timer, ^(){
[self stopHolesTimer];

[self setMaskPath:self.holePaths skipAnimation:NO];
});
dispatch_resume(timer);

_holesTimer = timer;
}
}
Expand All @@ -202,14 +204,14 @@ -(void)stopHolesTimer

-(void)setMaskPath:(UIBezierPath *)maskPath skipAnimation:(BOOL)skipAnimation{
UIBezierPath *oldPath = _maskPath;

_maskPath = maskPath;

if(!skipAnimation && self.animation){
[_maskLayer removeAnimationForKey:@"path"];

CABasicAnimation *pathAnimation = [CABasicAnimation new];

pathAnimation.duration = _animationDuration.doubleValue/1000.0;
pathAnimation.keyPath = @"path";
pathAnimation.fromValue = oldPath ? (id)oldPath.CGPath : (id)_maskLayer.path;
Expand All @@ -218,34 +220,34 @@ -(void)setMaskPath:(UIBezierPath *)maskPath skipAnimation:(BOOL)skipAnimation{
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.timingFunction = _animationTimingFunction;
pathAnimation.delegate = self;

[_maskLayer addAnimation:pathAnimation forKey:@"path"];
}else{
_maskLayer.path = _maskPath.CGPath;
}

}

- (UIBezierPath *)holePaths
{
UIBezierPath *currentPath = [UIBezierPath new];
currentPath.usesEvenOddFillRule = YES;

[_parsedHoles enumerateObjectsUsingBlock:^(RNHoleViewHole *hole, NSUInteger idx, BOOL *_Nonnull stop) {
CGRect rect = hole.rect;

UIBezierPath *path = [UIBezierPath new];

[path addArcWithCenter:CGPointMake(rect.origin.x+rect.size.width-hole.borderTopRightRadius, rect.origin.y+hole.borderTopRightRadius) radius:hole.borderTopRightRadius startAngle:degreesToRadians(-90.f) endAngle:degreesToRadians(0.f) clockwise:YES];
[path addArcWithCenter:CGPointMake(rect.origin.x+rect.size.width-hole.borderBottomRightRadius, rect.origin.y+rect.size.height-hole.borderBottomRightRadius) radius:hole.borderBottomRightRadius startAngle:degreesToRadians(0.f) endAngle:degreesToRadians(90.f) clockwise:YES];
[path addArcWithCenter:CGPointMake(rect.origin.x+hole.borderBottomLeftRadius, rect.origin.y+rect.size.height-hole.borderBottomLeftRadius) radius:hole.borderBottomLeftRadius startAngle:degreesToRadians(90.f) endAngle:degreesToRadians(180.f) clockwise:YES];
[path addArcWithCenter:CGPointMake(rect.origin.x+hole.borderTopLeftRadius, rect.origin.y+hole.borderTopLeftRadius) radius:hole.borderTopLeftRadius startAngle:degreesToRadians(180.f) endAngle:degreesToRadians(270.f) clockwise:YES];

[currentPath appendPath:path];
}];

[currentPath appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height)]];

return currentPath;
}

Expand All @@ -255,21 +257,21 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
if ( [self pointInRects:point] || !self.userInteractionEnabled) {
return NO;
}

BOOL superPoint = [super pointInside:point withEvent:event];

return superPoint;
}


- (BOOL)pointInRects:(CGPoint)point
{
__block BOOL pointInPath = NO;

if (!CGPathContainsPoint(self.maskPath.CGPath, nil, point, YES) ) {
pointInPath = YES;
}

return pointInPath;
}

Expand All @@ -284,4 +286,15 @@ -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)finished{
}
}

- (void)setOnClick:(void (^)(void))onClickBlock {
_onClickBlock = onClickBlock;
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)]];
}

- (void)handleTap {
if (self.onClickBlock) {
self.onClickBlock();
}
}

@end