How to set a custom pointer effect on a button (Objective-C)?

Viewed 160

How to set a custom pointer effect on a button (Objective-C)?

backToProjectsButton.pointerInteractionEnabled = YES;
        UITargetedPreview *preview = [[UITargetedPreview alloc] initWithView:backToProjectsButton];
        UIPointerStyle *buttonPointerStyle = [UIPointerStyle styleWithEffect:[UIPointerHighlightEffect effectWithPreview:preview] shape:[UIPointerShape shapeWithRoundedRect:backToProjectsButton.bounds]];
        
        backToProjectsButton.pointerStyleProvider = /* NOT SURE WHAT COMES NEXT */
1 Answers

OK, after some trial and error, I figured it out! The code is below.

backToProjectsButton.pointerStyleProvider = ^UIPointerStyle*(UIButton *button, UIPointerEffect *proposedEffect, UIPointerShape *proposedShape){
            CGRect buttonRect = CGRectInset(button.bounds, -12, -14);
            buttonRect = [button convertRect:buttonRect toView:proposedEffect.preview.target.container];
            UIPointerStyle *buttonPointerStyle = [UIPointerStyle styleWithEffect:[UIPointerHighlightEffect effectWithPreview:proposedEffect.preview] shape:[UIPointerShape shapeWithRoundedRect:buttonRect]];
            return buttonPointerStyle;
        };
Related