放大和缩小UIView

时间:2011-02-17 22:21:17

标签: xcode ios zoom zooming

使用简单的方法按钮放大和缩小UIView的最佳方法是什么。 (e.i

(IBAction)zoomin:(int)distance
{
method here
}
(IBAction)zoomout:(int)distance
{
and here
}

2 个答案:

答案 0 :(得分:27)

可以使用两个手指手势识别器完成: 你必须写下来: -

-(void)viewDidLoad
{
UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc] 
                                           initWithTarget:self 
                                           action:@selector(twoFingerPinch:)] 
                                           autorelease];

[[self view] addGestureRecognizer:twoFingerPinch];
}

通过这个你已经初始化了一个实例,它将在屏幕上处理两个手指感觉(或者你应用这个方法的视图) 现在定义如果你已经识别出两个手指该怎么做:

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{
    NSLog(@"Pinch scale: %f", recognizer.scale);
    CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
                                      // you can implement any int/float value in context of what scale you want to zoom in or out
    self.view.transform = transform;
}

上面定义的方法不是通过UIButton操作自动调用,而是简单地解决您的问题  如果您只想在IBAction上使用缩放,那么只需执行以下操作:

 -(IBAction)methodCalledOnClickingUIButton:(id)sender
{
    if(sender==zoomInButton)
     {
       scaleValue++;
     }
    else if(sender==zoomOutButton)
     {
       scaleValue--;
     }
     CGAffineTransform transform = CGAffineTransformMakeScale(scaleValue,scaleValue);
     self.view.transform = transform;
}

scaleValue是浮动值的任何值......您可以根据应用需求进行设置! 我希望它对你有用! :)

答案 1 :(得分:1)

Swift 3,4 +

用两根手指检测放大/缩小UIView。这是一个听主视图的例子:

override func viewDidLoad() {    
     var pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchedView))
     view.isUserInteractionEnabled = true
     view.addGestureRecognizer(pinchGesture)    
}

// Listener

@objc func pinchedView(sender: UIPinchGestureRecognizer) {
    if sender.scale > 1 {
        print("Zoom out")
    } else{
        print("Zoom in")
    }
}