0

请建议我,scrollview一次又一次地重新绘制元素,而不是像 iOS 6 下的默认值一样旋转view,这样是否很好。我的应用程序支持 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,或者重绘元素是最好的选择?任何这样的例子都会非常好。

4

1 回答 1

1

不,您遵循的方法会导致性能问题。redrawing你不是recreating子视图。重新创建整个子视图层次结构只是为了让它们具有不同的大小并不是一个好的编程。

理想情况下,在设备旋转时,仅根据方向更改滚动视图和子视图的框架。处理此问题的一种方法是 subclassUIScrollView和 overridelayoutSubview方法,您可以在其中重新布局(仅更改框架而不重新创建)子视图。否则,您可以枚举方法UIScrollView中的子视图didRotate:并根据特定方向重新构建它们。

于 2013-06-14T11:55:02.897 回答