如何在目标C中在运行时添加动态大小的可移动UIView作为子视图?

时间:2011-12-12 09:34:41

标签: objective-c uiview uitouch

我需要添加一个UIView作为UIView的子视图,其中显示UIImageview。我需要实现的是,当我的UIIMageview中加载图像时,同时我显示UIView将其称为newView。一开始,我用静态大小表示190X170,alpha表示为0.5。一旦我在那个视图上轻拍我的手指,它应该在整个UIImageview上移动。一旦它在最后移动,我正在协调并用相同的点裁剪我的图像。现在我使用下面的代码完成了移动部件。

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

我需要实现的是调整UIView的大小,如同UIScrollview那样。我试图实现UIScrollview,但它没有给我带来好的效果。我试着实施  NSUInteger resizingMask = NSViewHeightSizable | NSViewWidthSizable; [self.view setAutoresizesSubviews:YES]; [newView setAutoresizingMask:resizingMask]; 但是徒劳无功。什么都没发生。

任何人都可以告诉我一条相同的道路。提前谢谢。

1 个答案:

答案 0 :(得分:1)

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

这是非常酷的教程,希望它对你有所帮助。

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