0

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.

user3266053
  • 175
  • 1
  • 1
  • 10
  • Where is imageView allocated? – maelswarm Jul 05 '16 at 23:41
  • Thank you for your answer Roecrew. I don´t understand what you mean. The code i am using in the *.m file are the code above. In the *.h file i have this `@property(nonatomic,retain) IBOutlet UIImageView *imageView;` and `@property (nonatomic, retain) IBOutlet UIView *top;` – user3266053 Jul 06 '16 at 03:41

1 Answers1

1

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;


    }
jack
  • 11
  • 2
  • Thanks Jack, The problem with UIPanGestureRecognizer is that i want to use `NSLog(@"%@", NSStringFromCGPoint(touchLocation));` and i don't know have to read the position with `UIPanGestureRecognizer` Why i want that is because i want to read where the UIImageView mask position are. – user3266053 Jul 06 '16 at 11:13
  • I know only how many uiimageview on UIView . then u take all uiimageview in array and add gesture to one by one with tag. then check – jack Jul 06 '16 at 11:44
  • Okej, but i don't get your code to work. I tried to put the code that is over the `-(void)moveImageWithGesture:(UIPanGestureRecognizer *)panGesture` in viedDidLoad and i also tried to put the code in `- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer` but don't get it to work. You have enter a comment... image and frame? I only have one UIImageView for the mask and then i have a imageview for the background image. – user3266053 Jul 06 '16 at 11:51
  • Thank you jack for your time! – user3266053 Jul 06 '16 at 11:56
  • -(void)moveImageWithGesture:(UIPanGestureRecognizer *)panGesture this method define outside the ViewDidLoad – jack Jul 06 '16 at 11:59
  • 1
    http://stackoverflow.com/questions/5757386/how-to-mask-an-uiimageview use this link – jack Jul 06 '16 at 12:02