自定义UIView如何处理屏幕旋转

时间:2017-12-16 03:24:57

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

出于某种原因,我制作了一个自定义视图,覆盖它的绘制画面menthod,绘制一些字符串和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

将其添加到viewController

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

它工作正常。

然而,当屏幕旋转时,它会清楚地看到字符串过渡不平滑但很难看。

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

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

1 个答案:

答案 0 :(得分:0)

只需将dependencies { compile 'com.google.android.gms:play-services-plus:9.0.2' compile 'com.google.android.gms:play-services-ads:9.0.2' compile 'com.google.android.gms:play-services-gcm:9.0.2' compile 'com.google.android.gms:play-services-maps:9.0.2' compile 'com.google.android.gms:play-services-auth:9.0.2' compile 'com.google.android.gms:play-services-base:9.0.2' compile 'com.google.android.gms:play-services-identity:9.0.2' compile 'com.google.api-client:google-api-client:1.23.0' compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.google.code.gson:gson:2.4' // V2: Endpoints Framework v2 migration endpointsServer project(path: ':DeviceLocatorBackend', configuration: 'endpoints') compile 'com.google.api-client:google-api-client:+' compile 'com.google.http-client:google-http-client-android:1.22.0' } 设置为contentMode,默认模式UIViewContentModeRedraw即可缩放内容。