【问题标题】:How custom UIView handle screen rotation自定义 UIView 如何处理屏幕旋转
【发布时间】:2017-12-16 03:24:56
【问题描述】:

出于某种原因,我制作了一个自定义视图,覆盖它的 drawrect 方法,以绘制一些字符串和 img。就像这样:

@implementation WaterMarkView

- (void)drawRect:(CGRect)rect {
    NSLog(@"drawRect");

    NSString* test = @"This is a test string";
    NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:22],
                           NSForegroundColorAttributeName:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0],
                           };

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    CGSize strsize = [[[NSAttributedString alloc] initWithString:test attributes:attributes] size];

    CGRect screentBounds = [[UIScreen mainScreen] bounds];
    [test drawAtPoint:CGPointMake((screentBounds.size.width - strsize.width)/2,
                                  (screentBounds.size.height - strsize.height)/2) withAttributes: dict];
}


@end

将其添加到视图控制器

....
WaterMarkView* view = [[WaterMarkView alloc] init];
view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:view];
....

效果很好。

但是,当屏幕旋转时,会明显看到字符串过渡不流畅,而且很丑。

当我添加一个 UILable whith 字符串时,它会在屏幕旋转时平滑过渡

如何在屏幕旋转时使自定义 UIView 过渡平滑?

【问题讨论】:

    标签: ios objective-c uiview screen-rotation uianimation


    【解决方案1】:

    只需将contentMode设置为UIViewContentModeRedraw,默认模式UIViewContentModeScaleToFill会缩放内容。

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 1970-01-01
      相关资源
      最近更新 更多