【问题标题】:Rotate uiscrollview when changed device orientation更改设备方向时旋转 uiscrollview
【发布时间】:2013-06-14 09:28:04
【问题描述】:

请建议我,用元素一次又一次地重绘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 的方向,或者重绘元素是最好的选择?任何这样的例子都会非常好。

【问题讨论】:

    标签: ios objective-c ios4 label scrollview


    【解决方案1】:

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

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

    【讨论】:

    • 阿马尔,谢谢您的回答!不幸的是,我不完全理解如何使用 UIScrollView 子类的 layoutSubview 并遵循您的回答,而不是 init,我为 uiscrollview 和标签制作 cgrectmake,所以现在我不需要 removeFromSuperview 并添加这个 scrollView 来查看。否则我不会更改 didRotate:,但代码变得更小并且一切正常。谢谢!!!
    • 在我的情况下,我添加:- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return YES;//(interfaceOrientation == UIInterfaceOrientationPortrait); } 以及: -(void)viewDidLayoutSubviews { myScroll.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); }
    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多