[英]UIView stretching side instead of adjusting width & origin
這可能是一個簡單的問題,我不知道是否可能; 但仍然很容易。
Screen = 0x 0y 1000w 1000h
UIView Start = 500x 0y 500w 1000h
UIView End = 0x 0y 1000w 1000h
我有一個 UIView 可以說用上面的尺寸和坐標覆蓋屏幕的右側。 現在我知道要達到我必須做的終點
CGRectMake(start.origin.x - 500, start.origin.y, start.size.width + 500, start.size.height)
是否有可能只做類似的事情
[uiview stretchLeftSide:500];
這樣我就不需要直接觸摸原點或寬度。 它會導致一些問題,因為視圖包含我無法管理錨定的控件,因此它們只是跳轉到指定位置,然后視圖會調整(這都是動畫順便說一句)。
據我所知,以這種方式重新定位和調整UIView
- CGRectMake(start.origin.x - 500, start.origin.y, start.size.width + 500, start.size.height)
是這樣做的方法。
這很丑陋,但這是我以前做的。 但是你可以繼承UIView
並添加一個方法stretchLeftSide
並實現你想要的。 在長期運行的項目中,這是絕對有意義的,應該作為一種練習來完成......
首先,我假設您的UIView
(我們稱之為視圖)是Screen
的子視圖(即您做了類似[Screen addSubview:view]
的事情)。
如果你想做 animation 所以左側伸展你想要的方式,基本上,你所要做的就是將view
的原點設置為(0,0)
並且大小與Screen
相同。 為此,您可以使用Screen.bounds
,這正是您想要的最終view
框架。 視圖的bounds
屬性將始終具有(0,0)
的原點和等於視圖大小的大小。
如果你有對Screen
的引用,你可以使用我上面提到的Screen.bounds
,但如果你沒有,並且你知道view
是Screen
的子視圖,那么你可以使用view.superview.bounds
。
現在是 animation。 因為我必須編寫支持 iOS 3.0 版本的應用程序,所以我不能使用塊動畫。 相反,我使用UIView
class 方法,如下所示:
// Here you can enter an identifier to identify your animation
// and an object that will be passed to your delegate.
// These 2 parameters are only useful if you set an animation delegate
// using [UIView setAnimationDelegate:delegate];
[UIView beginAnimations:nil context:nil];
// Duration in seconds
[UIView setAnimationDuration:0.5];
// Perform you changes here.
view.frame = view.superview.bounds;
// Commit the changes
[UIView commitAnimations];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.