0

ストーリーボードとプログラムで生成されたボタンとラベルを使用してビューコントローラー内にビューを生成するアプリで、ユーザーがピンチを使用してズームできるようにしたいと考えています。ビューの属性インスペクターで、 と の両方をチェックUser Interaction EnabledMultiple Touchました。しかし、ズームするピンチは機能していないようです。

View の重要な部分を作成するコードは次のとおりです。

- (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];
    }

}

このアプリでピンチ ツー ズームを有効にするにはどうすればよいですか?

4

2 に答える 2

4

ピンチしてズームできるようにしたい場合は、最初UIViewにラップする必要がありますUIScrollView

于 2013-08-07T14:10:06.563 に答える
2

そのように使用User Interaction Enabledして試してみることもできますが、 に を追加する方が簡単UIGestureRecogniserですUIView。このようにして、ほとんどのセットアップ コードを処理し、ピンチを検出するとメソッドをトリガーします。

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);
}

そしてスウィフトでは:

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)")
}
于 2013-08-07T14:13:38.230 に答える