문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top