[英]Basic UIImageView Question
大家好,當您設置UIImageView的坐標時,會將UIImageView設置為右上角。 有什么辦法可以將坐標應用於左下角?
如果您有該視圖的引用,則可以設置其錨點uisng
view.layer.anchorPoint = CGPointMake(0,1);
//您的左下角。
0,0表示左上角
默認值為0.5,0.5,即居中
最大值是1,1,位於右下角
並且不要忘記導入<QuartzCore/QuartzCore.h>
從UIView
繼承的UIImageView
定位設置。 這就是為什么您只能按UIImageView
frame
和center
屬性進行操作的原因。 但是您可以“模擬”左下角的定位:
UIView *yourView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
int bottomLeftX, bottomLeftY;
CGRect rect = yourView.frame;
yourView.frame = CGRectMake(bottomLeftX, bottomLeftY - rect.size.height, rect.size.width, rect.size.height);
回答第二個問題...只需將這些文件導入您要創建uiimageview的位置
@interface UIImageView (MyCategory)
- (id)init;
@end
@implementation UIImageView (MyCategory)
- (id)init {
self = [super init];
if (self) {
self.layer.anchorPoint = CGPointMake(0, 1);
}
return self;
}
@end
上面的代碼可能會導致問題,因為UIImageView的init可能也在執行其他任務,因此您應該這樣做
@interface UIImageView (MyCategory)
- (id)initMyImageView;
@end
@implementation UIImageView (MyCategory)
- (id)initMyImageView {
[self init];
self.layer.anchorPoint = CGPointMake(0, 1);
return self;
}
@end
並像[[UIImageView alloc] initMyImageView];
一樣使用它[[UIImageView alloc] initMyImageView];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.