首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现: UIViewContentMode .重复一遍?

如何实现: UIViewContentMode .重复一遍?
EN

Stack Overflow用户
提问于 2012-09-10 21:51:54
回答 1查看 666关注 0票数 0

UIViewContentMode涵盖了您经常需要的几个职位(中心、ScaleToFill、ScaleToFit),以及我怀疑大多数人很少使用的负载(TopRight,有人吗?)

但它似乎缺少了一个明显的例子:“重复”。

有办法有效地重复UIView的内容吗?也就是说,平铺视图,当你调整尺寸时,它只是发现/覆盖了更多的平铺内容?

(显然,我说的不是UIImageViews - UIImage/UIColor有一种处理位图数据的方法,但这是另外一个问题。我说的是UIView,意思是“drawRect”

EN

回答 1

Stack Overflow用户

发布于 2012-09-11 00:44:30

这是我的基本实现,手工操作。这可能是可怕的低性能(想必:它迫使视图重绘,而不是缓存输出?)

TilingView.h

代码语言:javascript
复制
@interface TilingView : UIView

@property( nonatomic, retain ) UIView* templateView;

@end

TilingView.m

代码语言:javascript
复制
@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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12359972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档