Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 启用页面并更改设备旋转/方向的UIScrollView(疯狂)_Objective C_Cocoa Touch_Ios_Uiscrollview_Uiinterfaceorientation - Fatal编程技术网

Objective c 启用页面并更改设备旋转/方向的UIScrollView(疯狂)

Objective c 启用页面并更改设备旋转/方向的UIScrollView(疯狂),objective-c,cocoa-touch,ios,uiscrollview,uiinterfaceorientation,Objective C,Cocoa Touch,Ios,Uiscrollview,Uiinterfaceorientation,我很难做到这一点 我有一个UIScrollView,启用了分页。它由一个视图控制器(MainViewController)管理,每个页面由一个PageViewController管理,它的视图作为scrollView的子视图以适当的偏移量添加。滚动是左右滚动,用于标准方向的iPhone应用程序。效果很好。基本上与苹果提供的示例完全相同,也与iPhone提供的天气应用程序完全相同 然而,当我试图支持其他方向时,事情并不是很好。我使用以下方法支持MainViewController和PageView

我很难做到这一点

我有一个UIScrollView,启用了分页。它由一个视图控制器(MainViewController)管理,每个页面由一个PageViewController管理,它的视图作为scrollView的子视图以适当的偏移量添加。滚动是左右滚动,用于标准方向的iPhone应用程序。效果很好。基本上与苹果提供的示例完全相同,也与iPhone提供的天气应用程序完全相同

然而,当我试图支持其他方向时,事情并不是很好。我使用以下方法支持MainViewController和PageViewController中的每个方向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     return YES;
}
但是,当我旋转设备时,我的页面会变得非常倾斜,并且会出现很多绘图问题,特别是如果只加载了部分页面,那么我会旋转,然后滚动更多,等等。。。很乱

我已经告诉我的视图支持使用自动调整大小

 theView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
但是没有用。它似乎只是延伸和扭曲了我的观点

在我的MainViewController中,我添加了这一行,试图调整我所有页面的视图大小:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * ([self.viewControllers count]), self.scrollView.frame.size.height);

    for (int i = 0; i < [self.viewControllers count]; i++) {
        PageViewController *controller = [self.viewControllers objectAtIndex:i];
        if ((NSNull *)controller == [NSNull null])
            continue;

        NSLog(@"Changing frame: %d", i);
        CGRect frame = self.scrollView.frame;
        frame.origin.x = frame.size.width * i;
        frame.origin.y = 0;
        controller.view.frame = frame;
    }
}
-(void)从InterfaceOrientation(接口方向)中删除了RotateFromInterfaceOrientation:(UIInterfaceOrientation){
self.scrollView.contentSize=CGSizeMake(self.scrollView.frame.size.width*([self.viewControllers count]),self.scrollView.frame.size.height);
对于(int i=0;i<[self.viewControllers count];i++){
PageViewController*controller=[self.viewController对象索引:i];
如果((NSNull*)控制器==[NSNull])
继续;
NSLog(@“更改帧:%d”,i);
CGRect frame=self.scrollView.frame;
frame.origin.x=frame.size.width*i;
frame.origin.y=0;
controller.view.frame=frame;
}
}
但这并没有太大帮助(因为我懒散地加载视图,所以在执行时并不一定加载所有视图)


有什么办法解决这个问题吗?

我不太确定我是否完全理解你的意思。。然而,一些想法:

frame属性是一件事(A),视图内容在其中的显示方式是另一件事(B)。帧CGRect是superview(父视图)中视图的(理论)边界。。但是,视图不一定需要填充整个帧区域

关于(A)项: 这里我们有UIView的
autoresizingMask
属性来设置调整superview大小时如何调整帧大小。当你改变方向时会发生这种情况。但是,您通常可以依赖默认设置(到目前为止对我有效)

关于(B): 视图内容在图幅中的分布方式由UIView的属性
contentMode
指定。使用此属性,可以设置纵横比需要保持不变。例如,将其设置为
UIViewContentModeScaleAspectFit
或其他

请看这里:


PS:我写了“理论”,因为您的视图内容也可能超出这些帧边界-它们仅在UIView的clipsToBounds属性设置为“是”时限制视图。我认为苹果默认设置为“否”是一个错误。

除了Efrain所写的,请注意,如果视图变换不是标识变换,即视图旋转时,frame属性无效


当然,您考虑了视图需要位于新的偏移位置这一事实,对吗?

是否需要为UIScrollView中的每个页面使用单独的UIViewController(PageViewController)?为什么不让MainViewController处理这个问题呢


在Interface Builder中构建UI时,旋转设备后调整视图(以及用户界面)的大小要容易得多。

我使用以下方法成功地实现了这一点:

.h文件代码:

@interface ScrollViewController2 : UIViewController <UIWebViewDelegate, UIScrollViewDelegate> {
NSMutableArray *views;
int currentPage;

IBOutlet UIScrollView *scrollView;
BOOL bolPageControlUsed;
int intCurrIndex;

NSMutableArray *arrayContentData;
NSMutableArray *viewControllers;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;

@property (nonatomic, retain) NSMutableArray *arrayContentData;
@property (nonatomic, retain) NSMutableArray *viewControllers;
@property (nonatomic) BOOL bolPageControlUsed;
@property (nonatomic) int intCurrIndex;

-(void)bindPages;

- (void)setUpScrollView;
- (void)alignSubviews;

- (NSURLRequest *)getPageFromDocumentsDirectory:(NSString *)pstrPageName;

-(void)initiateScrollView;
-(void)loadScrollViewWithPage:(int)page;
@界面滚动视图控制器2:UIViewController{
NSMutableArray*视图;
int当前页面;
IBUIScrollView*滚动视图;
使用布尔布尔函数;
国际货币指数;
NSMutableArray*arrayContentData;
NSMutableArray*视图控制器;
}
@属性(非原子,保留)IBMOutlet UIScrollView*scrollView;
@属性(非原子,保留)NSMutableArray*arrayContentData;
@属性(非原子,保留)NSMUTABLEARRY*viewControllers;
@使用的属性(非原子)BOOL bolpagecontrol;
@属性(非原子)intCurrIndex;
-(作废)活页夹;
-(无效)设置滚动视图;
-(无效)对齐子视图;
-(NSURLRequest*)getPageFromDocumentsDirectory:(NSString*)pstrPageName;
-(void)initiateScrollView;
-(void)加载滚动视图带页面:(int)页面;
============================================================================================

.m文件 @综合滚动视图

@合成arrayContentData、ViewController、bolPageControlUsed、intCurrIndex

- (void)viewDidLoad {
[super viewDidLoad];

[self bindPages];

//[self setUpScrollView];

[self initiateScrollView];
}

#pragma mark -
#pragma mark Bind Pages
-(void)bindPages{
self.arrayContentData = [[NSMutableArray alloc] init];

[self.arrayContentData addObject:@"1.html"];
[self.arrayContentData addObject:@"2.html"];
[self.arrayContentData addObject:@"3.html"];
[self.arrayContentData addObject:@"4.html"];
[self.arrayContentData addObject:@"5.html"];
[self.arrayContentData addObject:@"6.html"];

[self.arrayContentData addObject:@"1.html"];
[self.arrayContentData addObject:@"2.html"];
[self.arrayContentData addObject:@"3.html"];
[self.arrayContentData addObject:@"4.html"];
[self.arrayContentData addObject:@"5.html"];
[self.arrayContentData addObject:@"6.html"];

[self.arrayContentData addObject:@"1.html"];
[self.arrayContentData addObject:@"2.html"];
[self.arrayContentData addObject:@"3.html"];
[self.arrayContentData addObject:@"4.html"];
[self.arrayContentData addObject:@"5.html"];
[self.arrayContentData addObject:@"6.html"];

[self.arrayContentData addObject:@"1.html"];
[self.arrayContentData addObject:@"2.html"];
[self.arrayContentData addObject:@"3.html"];
[self.arrayContentData addObject:@"4.html"];
[self.arrayContentData addObject:@"5.html"];
[self.arrayContentData addObject:@"6.html"];
}

#pragma mark - 
#pragma mark Get Filename from Document Directory
- (NSURLRequest *)getPageFromDocumentsDirectory:(NSString *)pstrPageName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *yourFilePath = [NSString stringWithFormat:@"%@/Html/%@", documentDirectory, pstrPageName];
NSURL *url = [NSURL fileURLWithPath:yourFilePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
return requestObj;
}


#pragma mark -
#pragma mark ScrollView Methods
-(void)initiateScrollView{
views = [[NSMutableArray alloc] initWithCapacity:[self.arrayContentData count]];

NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [self.arrayContentData count]; i++) {
    [controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];

scrollView.contentSize = CGSizeMake([self.arrayContentData count]*scrollView.bounds.size.width,
                                    scrollView.bounds.size.height);
scrollView.delegate = self;

if(self.intCurrIndex == 0){
    [self loadScrollViewWithPage:self.intCurrIndex];
}
}
-(void)loadScrollViewWithPage:(int)page{
if (page < 0) return;
if (page >= [self.arrayContentData count]) return;

// replace the placeholder if necessary
NSString *strContentName = [self.arrayContentData objectAtIndex:page];

//UIImageView *controller = [viewControllers objectAtIndex:page];
UIWebView *controller = [viewControllers objectAtIndex:page];

if ((NSNull *)controller == [NSNull null]) {

    UIView *v = [[UIView alloc] initWithFrame:scrollView.bounds];
    v.backgroundColor = [UIColor colorWithHue:arc4random()/(float)0x100000000
                                   saturation:0.75
                                   brightness:1.0
                                        alpha:1.0];

    controller = [[UIWebView alloc] initWithFrame:v.bounds];
    controller.delegate = self;
    controller.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    controller.center = CGPointMake(v.bounds.size.width/2, v.bounds.size.height/2);
    [controller loadRequest:[self getPageFromDocumentsDirectory:strContentName]];
    [v addSubview:controller];
    [controller release];

    [scrollView addSubview:v];

    [views addObject:v];
    [viewControllers replaceObjectAtIndex:page withObject:controller];
    [v release];
}

[self alignSubviews];

/*
// add the controller's view to the scroll view
if (nil == controller.superview) {
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    //frame.origin.y = 0;
    controller.frame = frame;
    [scrollView addSubview:controller];
}*/
}
-(void)scrollViewDidScroll:(UIScrollView *)sender{
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (self.bolPageControlUsed) {
    // do nothing - the scroll was initiated from the page control, not the user dragging
    return;
}
// Switch the indicator when more than 50% of the previous/next page is visible

currentPage = scrollView.contentOffset.x / scrollView.bounds.size.width;
[self loadScrollViewWithPage:currentPage];
}
-(void)viewDidLoad{
[超级视图下载];
[自动装订页];
//[自设置滚动视图];
[自启动滚动视图];
}
#布拉格标记-
#pragma标记绑定页
-(作废)活页夹{
self.arrayContentData=[[NSMutableArray alloc]init];
[self.arrayContentData addObject:@“1.html”];
[self.arrayContentData addObject:@“2.html”];
[self.arrayContentData addObject:@“3.html”];
[self.arrayContentData addObject:@“4.html”];
[self.arrayContentData addObject:@“5.html”];
[self.arrayContentData addObject:@“6.html”];
[self.arrayContentData addObject:@“1.html”];
[self.arrayContentData addObject:@“2.html”];
[self.arrayContentData addObject:@“3.html”];
[self.arrayContentData addObject:@“4.html”];
[self.arrayContentData addObject:@“5.html”];
[self.arrayContentData addObject:@“6.html”];
[self.arrayContentData addObject:@“1.html”];
[self.arrayContentData addObject:@“2.html”];
[self.arrayContentData addObject:@“3.html”];
[self.arrayContentData addObject:@“4.html”];
[self.arrayContentData addObject:@“5.html”];
[self.arrayContentData addObject:@“6.html”];
[self.arrayContentData addObject:@“1.html”];
[self.arrayContentData addObje]
#pragma mark -
#pragma mark setUp ScrollView
- (void)setUpScrollView {
// Set up some colorful content views
views = [[NSMutableArray alloc] initWithCapacity:[self.arrayContentData count]];

for (int i = 0; i < [self.arrayContentData count]; i++) {
    UIView *v = [[UIView alloc] initWithFrame:scrollView.bounds];
    v.backgroundColor = [UIColor colorWithHue:arc4random()/(float)0x100000000
                                   saturation:0.75
                                   brightness:1.0
                                        alpha:1.0];

    NSString *strContentName = [self.arrayContentData objectAtIndex:i];

    UIWebView *controller = [[UIWebView alloc] initWithFrame:v.bounds];
    controller.delegate = self;
    controller.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    controller.center = CGPointMake(v.bounds.size.width/2, v.bounds.size.height/2);
    [controller loadRequest:[self getPageFromDocumentsDirectory:strContentName]];
    [v addSubview:controller];
    [controller release];

    [scrollView addSubview:v];

    [views addObject:v];
    [v release];
}

[self alignSubviews];

[scrollView flashScrollIndicators];
}

#pragma mark -
#pragma mark Align Scroll Subviews
- (void)alignSubviews {
// Position all the content views at their respective page positions
scrollView.contentSize = CGSizeMake([self.arrayContentData count]*scrollView.bounds.size.width,
                                    scrollView.bounds.size.height);

NSUInteger i = 0;
for (UIView *v in views) {
    v.frame = CGRectMake(i * scrollView.bounds.size.width, 0,
                         scrollView.bounds.size.width, scrollView.bounds.size.height);

    for (UIWebView *w in v.subviews) {
        [w setFrame:v.bounds];
    }

    i++;
}
}

#pragma mark -
#pragma mark UIWebView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
- (void)webViewDidFinishLoad:(UIWebView *)webView { 
}


#pragma mark -
#pragma mark Orientation
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                            duration:(NSTimeInterval)duration {
currentPage = scrollView.contentOffset.x / scrollView.bounds.size.width;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
                                     duration:(NSTimeInterval)duration {
[self alignSubviews];
//NSLog(@"%f", currentPage * scrollView.bounds.size.width);
scrollView.contentOffset = CGPointMake(currentPage * scrollView.bounds.size.width, 0);
}