在UIImageView上添加UIPinchgesture的哪个比例尺或在UIVmageView中包含的UIView?

时间:2012-05-08 11:25:10

标签: iphone uiimageview uipinchgesturerecognizer

专家!! 请帮帮我!!!!! 在我的项目中,我以编程方式创建了多个UIImageviews。我想在每个ImageView.i上添加Pinch手势 我的代码在下面......

-(IBAction)addSymbols:(UIButton *)sender{
if (sender.tag ==1) {
CGRect frame =sender.frame;
[self.imgView setFrame:frame];
self.imgView.userInteractionEnabled = YES;
[imgView setImage:[sender imageForState:UIControlStateNormal]];
[self.view addSubview:imgView];
[self addGestureRecognizersToPiece:imgView];}

像这样我添加5个imageViews。现在代码为[self addGestureRecognizersToPiece:imgView]

- (void)addGestureRecognizersToPiece:(UIView *)piece{
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
[piece  addGestureRecognizer:pinchGesture];

//In above add gesture on UIImageView directly its not working..
//if replace this line by [self.view addGestureRecognizer:pinchGesture]; it's working..
//but while pinch whole UIview scale

[pinchGesture setDelegate:self]; }

另一个错误是UIPinchgesture无法单独正确处理多个UIImageViews。 所以请帮帮我..很快回复我..

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

if (paramSender.state == UIGestureRecognizerStateEnded) { 
self.currentScale = paramSender.scale;
} 
else if (paramSender.state == UIGestureRecognizerStateBegan && self.currentScale != 0.0f){ 

paramSender.scale = self.currentScale; }
 if (paramSender.scale != NAN && paramSender.scale != 0.0){ 
[paramSender view].transform= CGAffineTransformMakeScale(paramSender.scale, paramSender.scale); 
} 

正如您所说,我为每个UIImageView添加了userInteractionEnabled = YES。我在我的代码中也提到了

2 个答案:

答案 0 :(得分:0)

您应该在UIImageViews上启用userInteractionEnabled。默认情况下禁用此功能。

如果您需要更多帮助,请向我们展示您的scalePiece:代码。

答案 1 :(得分:0)

将imageViews添加为ScrollView的子视图。

然后设置scrollView的scrollView zoomScale。 还要实现scrollView的委托方法,尤其是下面的方法。

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

返回要缩放的imageView对象。

参考这些 UIScrollView

UIScrollViewDelegate