我已更新代码
我试图让UIImageView面具移动。有可能吗?
这是我想要开始工作的代码。
- (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];
}
}
如果我没有在viewDidLoad中使用遮罩代码,我可以移动imageView但是当我将代码添加到viewDidLoad时,imageView停止移动。
以下是新代码 现在我使用CAlayer来掩盖。
- (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];
}
移动我仍在使用的面具,但我希望在移动它时更新面具。当我移动它现在它只是相同的图像。
答案 0 :(得分:1)
在您的视图中使用此代码移动imageview
- (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;
}