簡體   English   中英

如何在目標C中在運行時將可移動UIView添加為具有動態大小的子視圖?

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

我需要添加一個UIView作為顯示UIImageview的UIView的子視圖。 我需要實現的是,當圖像加載到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;
}

我需要實現的是像UIScrollview一樣用兩根手指輕敲來調整UIView的大小。 我嘗試實現UIScrollview,但效果不佳。 我試圖實現NSUInteger resizingMask = NSViewHeightSizable | NSViewWidthSizable; [self.view setAutoresizesSubviews:YES]; [newView setAutoresizingMask:resizingMask]; NSUInteger resizingMask = NSViewHeightSizable | NSViewWidthSizable; [self.view setAutoresizesSubviews:YES]; [newView setAutoresizingMask:resizingMask]; 但徒勞無功。 什么也沒發生。

誰能給我展示一條通向我的道路。 提前致謝。

要提供放大縮小功能,可以使用UIPinchGuestureRecognizer。 如果您使用的是iOS 5,那么對您來說將非常容易。

這是非常酷的教程,希望對您有所幫助。

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