【问题标题】:How to implement: UIViewContentMode ... Repeat?如何实现:UIViewContentMode ...重复?
【发布时间】:2012-09-10 21:51:54
【问题描述】:
UIViewContentMode 涵盖了您经常需要的几个位置(Center、ScaleToFill、ScaleToFit),以及我怀疑大多数人很少使用的负载(TopRight,有人吗?)
但它似乎缺少一个明显的:“重复”。
有没有办法有效地重复 UIView 的内容?即一个平铺视图,当您调整它的大小时,它只会发现/覆盖更多的平铺内容?
(显然,我 不是在谈论 UIImageViews - UIImage/UIColor 有处理位图数据的方法,但这是另一个问题。我在谈论 UIView,意思是“drawRect” ...)
【问题讨论】:
标签:
ios
uikit
core-animation
quartz-graphics
【解决方案1】:
这是我的基本实现,手动。这可能是非常低的性能(大概:它强制视图重绘,而不是缓存输出?)
平铺视图.h
@interface TilingView : UIView
@property( nonatomic, retain ) UIView* templateView;
@end
平铺视图.m
@implementation TilingView
@synthesize templateView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
int cols = 1 + self.bounds.size.width / self.templateView.bounds.size.width;
int rows = 1 + self.bounds.size.height / self.templateView.bounds.size.height;
CGContextRef context = UIGraphicsGetCurrentContext();
for( int k=0; k<rows; k++ )
for( int i=0; i<cols; i++ )
{
CGContextSaveGState(context);
CGContextTranslateCTM(context, i * self.templateView.bounds.size.width, k * self.templateView.bounds.size.height);
[self.templateView drawRect:rect];
CGContextRestoreGState(context);
}
}
@end