Created
February 12, 2013 13:52
-
-
Save YoniTsafir/4770019 to your computer and use it in GitHub Desktop.
A useful method to mask a view according to an alpha channel to an image. Example usage:
place the masking image using IB (make it hidden), and dynamically create a view around it, that you mask using this method.
This file contains 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
+ (void)maskView:(UIView *)targetView withImageView:(UIImageView *)maskingImageView { | |
CALayer *maskingLayer = [CALayer layer]; | |
maskingLayer.frame = CGRectMake(maskingImageView.frame.origin.x - targetView.frame.origin.x, | |
maskingImageView.frame.origin.y - targetView.frame.origin.y, | |
maskingImageView.frame.size.width, | |
maskingImageView.frame.size.height); | |
maskingLayer.contents = (id)maskingImageView.image.CGImage; | |
targetView.layer.mask = maskingLayer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment