2011-12-12 41 views
1

我需要添加一个UIView作为显示UIImageview的UIView的子视图。我需要实现的是,当图像加载到我的UIIMageview中时,同时我显示UIView将其称为newView。一开始,我用静态大小190X170和alpha作为0.5显示它。一旦我点击我的手指在该视图上,它应该移动整个UIImageview。一旦它最后移动了,我就会采用相同的点来裁剪我的图像。现在我使用下面的代码完成移动部分。如何添加可移动UIView作为动态大小在运行时在目标C中的子视图?

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 
    if (CGRectContainsPoint(newView.frame, touchLocation)) 
    { 
     dragging = YES; 
     oldX = touchLocation.x; 
     oldY = touchLocation.y; 
    } 
    if (dragging) 
    {  
     newView.layer.borderColor = [UIColor clearColor].CGColor; 
     newView.layer.borderWidth = 1.0f; 
     CGRect frame = newView.frame; 
     frame.origin.x = newView.frame.origin.x + touchLocation.x - oldX; 
     x = frame.origin.x;   
     NSLog(@"x value : %d",x); 

     frame.origin.y = newView.frame.origin.y + touchLocation.y - oldY;  
     y = frame.origin.y; 
     NSLog(@"y value : %d",y); 
     newView.frame = frame; 
    } 
    oldX = touchLocation.x; 
    oldY = touchLocation.y; 
} 

我需要实现的是调整像UIScrollview那样的两根手指的UIView大小。我试图实现UIScrollview但它没有给我很好的效果。我试图实施 NSUInteger resizingMask = NSViewHeightSizable | NSViewWidthSizable; [self.view setAutoresizesSubviews:YES]; [newView setAutoresizingMask:resizingMask]; 但徒劳无功。没有任何事情发生。

任何人都可以为我显示相同的路径。提前致谢。

+0

总之你想实现图像裁剪功能? –

+0

是的同一件事。在放大newView时,我需要调整裁剪区域的大小。 – Sarah

回答

1

要提供放大缩小功能,您可以使用UIPinchGuestureRecognizer。如果你使用的是iOS 5,那么它对你来说很容易。

这里是非常酷的教程,希望它会帮助你。

http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more 
+0

谢谢Ajeet,但你以错误的方式提出了我的问题。我已经提到,我已经完成了移动UIView以及裁剪新视图协调的图像。我需要做的是,我需要用两根手指放大和缩小内部UIView,就像我们在UIScrollview中做的一样,这样我就可以为用户提供动态图像裁剪的功能。任何想法? – Sarah

+0

哦对不起,我正在更新我的答案 –

+0

谢谢ajeet。我浏览了代码,我认为它会拯救我的生命。但是我使用的是iOS 4.3,所以无法检查它是否工作。我不认为它可能会被苹果拒绝,如果它与大多数版本不兼容。情况如何? – Sarah

相关问题