简体   繁体   中英

How to pan inside diagram of UIView

Actually, i draw a diagrams like triangle, rectangle, pentagon in MyView which subclass of UIView. When i touch any point on MyView (ether that point inside diagram or not), MyView is moved. I would like to touch inside point of diagram then it has to be moved. I am using pan gesture recognizer on MyView.Please suggest to me.

My Code is:

In ViewController.m ,

- (void)viewDidLoad
{
    MyView *myView = [[MyView  alloc] initWithFrame:CGRectMake(0, 100, 200, 100)];
    [self.view addSubview:myView];
    [super viewDidLoad];
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
    [pinchGesture setDelegate:self];
    [myView addGestureRecognizer:pinchGesture];
    [pinchGesture release];
}

- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
        [gestureRecognizer setScale:1];
    }
}

In MyView.m ,

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    context =UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    // And draw with a blue fill color
    CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 2.0);

    CGContextMoveToPoint(context, 50.0, 10.0);  
    CGContextAddLineToPoint(context, 5.0, 70.0);  
    CGContextAddLineToPoint(context, 150.0, 55.0); 
    CGContextDrawPath(context, kCGPathFillStroke);
    CGContextClosePath(context);
    CGContextStrokePath(context);

}

So, you have code here that does a pinch gesture and you want to additionally do a pan gesture? If so, I would suggest you create the pan gesture in viewDidLoad:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(movePiece:)];
[myView addGestureRecognizer:panGesture];
[panGesture release];

Then add an ivar to your MyView class:

CGPoint _originalCenter;

And, finally, have the method to move your view:

- (void)movePiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan)
    {
        _originalCenter = [gestureRecognizer view].center;
    }
    else if ([gestureRecognizer state] == UIGestureRecognizerStateChanged) 
    {
        CGPoint translation = [gestureRecognizer translationInView:self.view];

        [gestureRecognizer view].center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y + translation.y);
    }
}

As an aside, a few observations regarding your code:

  1. You're setting your pinch gesture's delegate, but that's not necessary. That's accomplished by the initWithTarget method.

  2. In your drawRect method, I think you want to call CGContextClosePath before you call CGContextDrawPath .

Regardless, I hope I answered the question by showing you an example of how you could use a pan gesture to move your subview. (You said "I am using pan gesture..." but I assumed you meant "I would like to use pan gesture...".) If I misunderstood your question, then please clarify and rephrase the question and we can take another crack at answering it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM