我试图让一个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];
}
并移动我仍然使用的面具,但我希望面具更新,当我移动它。当我现在移动它只有相同的图像。
凡本代码移到imageview的是ImageView的分配呢? – Roecrew
谢谢你的回答Roecrew。 我不明白你的意思。 我在* .m文件中使用的代码是上面的代码。 在* .h文件中,我有这个@属性(nonatomic,retain)IBOutlet UIImageView * imageView;和@ @属性(nonatomic,retain)IBOutlet UIView * top;' – user3266053