Reputation: 175
I have update the code
I'm trying to get a UIImageView mask movable. Is it possible?
Here is the code i am trying to get to work.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect rect = CGRectMake(50, 50, 100, 100);
UIView *mask = [[UIView alloc] initWithFrame:rect];
mask.backgroundColor = UIColor.whiteColor;
mask.layer.cornerRadius = CGRectGetHeight(rect) / 2;
self.view.maskView = mask;
UIView *view = self.view;
UIColor *color = [UIColor colorWithRed:145.0 / 255.0
green:191.0 / 255.0
blue:192.0 / 255.0
alpha:1.0];
view.backgroundColor = color;
view.maskView = imageView;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == imageView) { // If touched view is imageView , then assign it its new location
imageView.center = touchLocation;
[self.view bringSubviewToFront:imageView];
}
}
If i don't have the mask code in the viewDidLoad i can move the imageView but when i add the code to the viewDidLoad the imageView stop being movable.
Here is the new code Now i am using CAlayer to mask.
- (void)viewDidLoad {
[super viewDidLoad];
CALayer *maskLayer = [CALayer layer];
UIImage *mask = [UIImage imageNamed:@"star.png"];
maskLayer.contents = mask;
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
imageView.image = [UIImage imageNamed:@"start.jpg"];
imageView.layer.mask = maskLayer;
[self.view addSubview:imageView];
imageView.userInteractionEnabled = YES;
maskLayer.contents = (id)mask.CGImage;
maskLayer.frame = (CGRectMake(60,60,300,300));
[self.view bringSubviewToFront:imageView];
}
And to move the mask i still using but i want the mask to update when i moving it. When i move it now its only the same image.
Upvotes: 0
Views: 470
Reputation: 11
Use this code move imageview on your view
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = CGRectMake(50, 50, 100, 100);
UIView *mask = [[UIView alloc] initWithFrame:rect];
mask.backgroundColor = UIColor.whiteColor;
mask.layer.cornerRadius = CGRectGetHeight(rect) / 2;
self.view.maskView = mask;
UIView *view = self.view;
UIColor *color = [UIColor colorWithRed:145.0 / 255.0
green:191.0 / 255.0
blue:192.0 / 255.0
alpha:1.0];
view.backgroundColor = color;
view.maskView = imageView;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizeralloc]initWithTarget:self action:@selector(moveImageWithGesture:)];
view.userInteractionEnabled = YES;
[view addGestureRecognizer:panGesture];
[self addSubview:view];
}
-(void)moveImageWithGesture:(UIPanGestureRecognizer *)panGesture
{
UIImageView *imageView = (UIImageView *)[panGesture view];
NSLog(@"%ld",(long)imageView.tag);
CGPoint touchLocation =[panGesture locationInView:self];
imageView.center = touchLocation;
}
Upvotes: 1