2016-07-05 43 views
0

我有更新可移动的UIImageView掩盖

我试图让一个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

凡本代码移到imageview的是ImageView的分配呢? – Roecrew

+0

谢谢你的回答Roecrew。 我不明白你的意思。 我在* .m文件中使用的代码是上面的代码。 在* .h文件中,我有这个@属性(nonatomic,retain)IBOutlet UIImageView * imageView;和@ @属性(nonatomic,retain)IBOutlet UIView * top;' – user3266053

回答

0

上使用您的视图

- (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; 


    } 
+0

谢谢杰克, UIPanGestureRecognizer的问题是我想要使​​用NSLog(@“%@”,NSStringFromCGPoint(touchLocation));'我不知道必须用'UIPanGestureRecognizer'读取位置为什么我想这是因为我想要读取UIImageView掩码位置是。 – user3266053

+0

我只知道UIView上有多少个uiimageview。然后你把数组中的所有uiimageview,并添加标签与一个接一个的手势。然后检查 – jack

+0

Okej,但我没有让你的代码工作。我试图把代码放在viedDidLoad中的' - (void)moveImageWithGesture:(UIPanGestureRecognizer *)panGesture'上,我也试着把代码放在' - (IBAction)handlePan:(UIPanGestureRecognizer *)识别器'让它工作。你有输入评论...图像和框架?我只有一个用于蒙版的UIImageView,然后我有一个用于背景图像的图像视图。 – user3266053