简体   繁体   中英

enable pinch to zoom in viewcontroller

I want users to be able to use pinch to zoom in my app which uses Storyboard and programmatically generated buttons and labels to produce a View within a Viewcontroller. In the View's Attribute inspector I have checked both User Interaction Enabled and Multiple Touch . But no pinch to zoom seems to work.

The code which creates the important part of the View is as follows.

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSInteger xPipsOrigin = 0;
    NSInteger xPipsStep = 22.0;
    NSString* cards = @"AKQJT98765432";
    NSInteger yPipsOrigin = 100;

    if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
        xPipsOrigin = 0;
        xPipsStep = 22.0;
        xPipsStep = 34.0;

    }
    else{
        xPipsOrigin = 100;
        xPipsStep = 40.0;
    }
    NSInteger xPipsCurrent = xPipsOrigin;

    self.countCards = 0;

    self.cardList = [NSMutableArray arrayWithCapacity:0];
    yPipsOrigin = 100;
    xPipsCurrent = xPipsOrigin;

    for( int x=0;x<[cards length]; x++ ){
        [cards substringWithRange:NSMakeRange(x,1)];
        UIButton *b= [UIButton buttonWithType:UIButtonTypeRoundedRect];
        xPipsCurrent += xPipsStep;
        [b setTitle:[cards substringWithRange:NSMakeRange(x,1)] forState:UIControlStateNormal];
        [b setTitle:@" " forState:UIControlStateDisabled];
        [b setFrame:CGRectMake(xPipsCurrent, yPipsOrigin, 20, 20)];
        [b setEnabled:YES];
        [b setUserInteractionEnabled:YES];
        [self.view addSubview:b];
        [b addTarget:self action:@selector(spadeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    }

    xPipsCurrent = xPipsOrigin + xPipsStep/2;
    for( int x=0;x<[cards length]-1; x++ ){
        xPipsCurrent += xPipsStep;
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(  0, 0, 20, 20)];
        lab.text = @"\u2660";
        lab.backgroundColor = [UIColor clearColor];
        lab.center = CGPointMake(xPipsCurrent+10, yPipsOrigin+10);
        [self.view addSubview:lab];
    }

}

How can I enable Pinch to Zoom in this app?

如果要启用缩放以放大UIView ,则必须先将其包装到UIScrollView

Whilst you can use User Interaction Enabled and try do it that way, it'll be easier to add a UIGestureRecogniser to your UIView . That way it handles most of the setup code for you, and when it detects a pinch triggers a method.

Basic example in Objective-C:

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];

- (void)handlePinchGesture:(id)sender
{
     NSLog(@"Recieved pinch");

     UIPinchGestureRecognizer *senderGR  = (UIPinchGestureRecognizer *)sender;
     NSLog(@"Scale is: %f", senderGR.scale);
}

And in Swift:

let pinchGestureRecognizer = UIPinchGestureRecognizer.init(target: self, action: #selector(ViewController.handlePinchGesture(_:)))
self.view.addGestureRecognizer(pinchGestureRecognizer)

func handlePinchGesture(sender:AnyObject) {
    print("Received pinch")
    let senderGR = sender as! UIPinchGestureRecognizer
    print("Scale is \(senderGR.scale)")
}

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