简体   繁体   English

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

[英]How to add movable UIView as subview with dynamic size at runtime in objective C?

I need to add a UIView as subview of a UIView where a UIImageview is displayed. 我需要添加一个UIView作为显示UIImageview的UIView的子视图。 What I need to implement is that when the image is loaded in my UIIMageview, at the same time I am showing the UIView call it as newView. 我需要实现的是,当图像加载到UIIMageview中时,同时我正在显示UIView称为newView。 In the beginning,I am showing it with static size say 190X170 and alpha as 0.5. 一开始,我以静态尺寸显示它,例如190X170,alpha为0.5。 Once i tap my finger on that view,it should move on the whole UIImageview. 一旦我在该视图上点击手指,它就应该在整个UIImageview上移动。 Once it is moved at the last,that coordinated I am taking and cropping my image with the same points. 一旦它最后移动了,那就协调了我用相同的点拍摄和裁剪我的图像。 Now I am done with the moving part using the code below. 现在,我使用下面的代码完成了运动部分。

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

What I need to implement is to resize the UIView on taping two fingers like that of UIScrollview. 我需要实现的是像UIScrollview一样用两根手指轻敲来调整UIView的大小。 I tried to implement the UIScrollview but it is not giving me good effect. 我尝试实现UIScrollview,但效果不佳。 I tried to implement NSUInteger resizingMask = NSViewHeightSizable | NSViewWidthSizable; [self.view setAutoresizesSubviews:YES]; [newView setAutoresizingMask:resizingMask]; 我试图实现NSUInteger resizingMask = NSViewHeightSizable | NSViewWidthSizable; [self.view setAutoresizesSubviews:YES]; [newView setAutoresizingMask:resizingMask]; NSUInteger resizingMask = NSViewHeightSizable | NSViewWidthSizable; [self.view setAutoresizesSubviews:YES]; [newView setAutoresizingMask:resizingMask]; but in vain. 但徒劳无功。 Nothing is taking place. 什么也没发生。

Can anybody show me a path for the same. 谁能给我展示一条通向我的道路。 Thanks in advance. 提前致谢。

To provide the zoom in zoom out feature you can use the UIPinchGuestureRecognizer. 要提供放大缩小功能,可以使用UIPinchGuestureRecognizer。 If you are using iOS 5 then it will be pretty easy for you. 如果您使用的是iOS 5,那么对您来说将非常容易。

here is the very cool tutorial for that hope it will help for you. 这是非常酷的教程,希望对您有所帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM