Pregunta

I'm new to iOS and need to have the needs below. Please give me some directions on how to do this. Thanks!

  • Have an image(source) masked by another image (frame)
  • User can drag,zoom, rotate the source image. The frame image keeps still.

I know how to use CGImageCreateWithMask and how to put uiimageview inside a scrollview for pinch to zoom. But I don't know how to combine these together and make a custom control.

ideal result

¿Fue útil?

Solución

To rotate an image you can use UIPanGestureRecognizer and to zoom in/out you can use UIPinchGestureRecognizer. May be the code below could help you.

// In your vieDidLoad use:

    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panned:)];

    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinched:)];
    [self.imageview addGestureRecognizer:pinch];
    [self.imageview addGestureRecognizer:pan];

    and add the selector methods:

    // To zoom in/out
    - (void)pinched:(UIPinchGestureRecognizer *)sender {

    if (sender.scale >1.0f && sender.scale < 2.5f) {
    CGAffineTransform transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
    imageview.transform = transform;
    }
    }

    //To rotate
    -(void) panned:(UIGestureRecognizer *)gesture
    {
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)gesture translationInView:imageview];

    if([(UIPanGestureRecognizer*)gesture state] == UIGestureRecognizerStateBegan) {
        _firstX = [imageview center].x;
        _firstY = [imageview center].y;
    }   
    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);  
    [imageview setCenter:translatedPoint];

    }

in your viewDidLoad your should also add your frame as as subview to the imageview.

Otros consejos

You could also put the frame image onto the scrollView that does the zooming, as a subview. That way, both images should zoom together.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top