请建议我,对于scrollview
一次又一次地重新绘制元素是一件好事,而不是仅仅以某种方式旋转所有view
,就像iOS 6中的默认值一样。我的应用支持iOS> 4.3。
有一些方法可以旋转scrollview
并在其上绘制元素。
- (void)viewDidLoad {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
[self didRotate:newOrientation];
[super viewDidLoad];
}
-(void)didRotate:(UIDeviceOrientation)orientation {
UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
CGRect frame = CGRectMake([UIScreen mainScreen].bounds.origin.x, [UIScreen mainScreen].bounds.origin.y, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
CGSize maximumSize;
if (UIInterfaceOrientationIsLandscape(newOrientation)) {
maximumSize = CGSizeMake(frame.size.width, frame.size.height);
} else {
maximumSize = CGSizeMake(frame.size.height, frame.size.width);
}
//self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.scrollView removeFromSuperview];
[name removeFromSuperview];
[text removeFromSuperview];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, maximumSize.width, maximumSize.height)];
self.scrollView.backgroundColor = [UIColor greenColor];
name = [self configureLabel:name WithCoordsOfX:100.0f y:10.0f Width:self.scrollView.bounds.size.width Height:10.0f forKey:@"title"];
text = [self configureLabel:text WithCoordsOfX:10.0f y:110.0f Width:self.scrollView.bounds.size.width-20 Height:self.scrollView.bounds.size.height forKey:@"text"];
self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width, text.frame.size.height+220);
text.backgroundColor = [UIColor grayColor];
// add myLabel
[self.scrollView addSubview:photo];
[self.scrollView addSubview:name];
[self.scrollView addSubview:text];
[self.view addSubview:self.scrollView];
NSLog(@"%f %f",self.scrollView.bounds.size.height,self.scrollView.bounds.size.width);
}
和
- (UILabel *)configureLabel:(UILabel *)label WithCoordsOfX:(float)x y:(float)y Width:(float)width Height:(float)height forKey:(NSString *)key
{
label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
label.text = [personData valueForKey:key];
[label setNumberOfLines:0];
[label setFont:[UIFont fontWithName:@"Georgia" size:14.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor blackColor]];
[label sizeToFit];
return label;
}
我尝试使用这行代码,但它对我没用。
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
其他视图在
之后旋转冷却- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
请告诉我是否有“好方法”改变UIScrollView
的方向,或者重绘元素是最佳选择?任何这方面的例子都会非常好。
答案 0 :(得分:1)
不,您关注的方法会导致性能问题。您不是redrawing
,而是recreating
子视图。重新创建整个子视图层次结构只是为了让它们具有不同的大小,这不是一个好的编程。
理想情况下,在设备旋转时,只根据方向更改scrollview和subviews的框架。处理此问题的一种方法是子类UIScrollView
,并覆盖layoutSubview
方法,您可以在其中重新布局(仅更改框架而不重新创建)子视图。另外,您可以在UIScrollView
方法中枚举didRotate:
子视图,并根据具体方向重新构建它们。