简体   繁体   English

是否可以从UIWebView对象滚动到UIView对象?

[英]Is it possible to scroll from UIWebView object to a UIView object?

In one of my app's user interfaces i want my app display a scrolling view(the count of the scrolled pages is dynamically set in runtime). 在我的应用程序的一个用户界面中,我希望我的应用程序显示一个滚动视图(滚动页面的数量在运行时动态设置)。 But i want the to add UIWebView and UIView elements to the same array, so that when i scroll i will be able to display both UIWebView(let's say a web page) and some other View? 但是我想将UIWebView和UIView元素添加到同一数组中,这样当我滚动时,我将能够同时显示UIWebView(比如说一个网页)和其他一些View?

EDIT 编辑

I received this answer which looks as if exactly ok but doesn't work(or i couldn't to) here is the code: NSArray *myArray; 我收到了这个答案,看起来好像还可以,但是不起作用(或者我不能),这里是代码:NSArray * myArray; // Fill the array with a bunch of UIWebView and UIView objects here //在此处用一堆UIWebView和UIView对象填充数组

for (id theObject in myArray)
{
    if ([theObject isKindOfClass:[UIWebView class]]) {
        // do stuff
    }
    else if ([theObject isKindOfClass:[UIView class]]) {
        // do other stuff
    }
}

Can anyone please give a hend of help? 谁能帮忙吗? I need to create a scrollable array of views(like the main page of the iPhone) but the first page(mean, view) must be a member of UIWebView (the others are UIView ) 我需要创建一个可滚动的视图数组(例如iPhone的主页),但第一页(均值,视图)必须是UIWebView的成员(其他是UIView

And below is all my code(i han't try to do anything here:) 下面是我的所有代码(我在这里不尝试做任何事情:)

  CGRect frame;
     [webView initWithFrame:frame];
    NSArray *colors = [NSArray arrayWithObjects:webView, [UIColor blackColor],[UIColor blackColor],  nil];
        for (int i = 0; i < colors.count; i++) {

        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;


        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [subview setTag:i];
        if([subview tag]==2){
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectMake(70.0f, 600.0f, 250.0f, 60);
            [button  setTitle:@"Read Messages" forState:UIControlStateNormal];
                for(NSDictionary* buttonData in butDat) { 
                UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                NSString* buttonTitle = [buttonData objectForKey:@"name"];
                NSString* buttonID=[buttonData objectForKey:@"order_number"];
                [button setTitle:buttonTitle forState:UIControlStateNormal];
                [button setTag:[buttonID intValue]];
                button.titleLabel.font=[UIFont systemFontOfSize:28];

                 CGFloat yPosition = 60.0f;
                const CGFloat buttonHeight = 85.0f;

                if (button.tag==1) {
                    button.frame = CGRectMake(70.0f, yPosition, 250.0f, buttonHeight);
                    //[ma.view addSubview:button];
                }
                if (button.tag==2) {
                    button.frame = CGRectMake(440.0f, yPosition, 250.0f, buttonHeight);
                    //[ma.view addSubview:button];   
                }
                if (button.tag==3) {
                    button.frame = CGRectMake(70.0f, 200.0f, 250.0f, buttonHeight);
                    //[ma.view addSubview:button];
                }
                if (button.tag==4) {
                    button.frame = CGRectMake(440.0f, 200.0f, 250.0f, buttonHeight);
                    //[menu_2.view addSubview:button];//****

                }
                if (button.tag==5) {
                    button.frame = CGRectMake(70.0f, 335.0f, 250.0f, buttonHeight);
                    //[menu_2.view addSubview:button];//****

                }
                if (button.tag==6) {
                    button.frame = CGRectMake(440.0f, 335.0f, 250.0f, buttonHeight);
                    //[menu_2.view addSubview:button];//****

                }

                [button addTarget:self action:@selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
                [scrollView addSubview:button];
            }
                //


            [button addTarget:self action:@selector(readM:) forControlEvents:UIControlEventTouchUpInside];
          //  [scrollView addSubview:button];
            [self.view addSubview:button];


            //  [self.scrollView addSubview:button];

        }
        [self.scrollView addSubview:subview];

There's nothing stopping you from adding objects of type UIWebView and UIView to the same NSArray . 没有什么可以阻止您将UIWebViewUIView类型的对象添加到同一NSArray

If your code needs to know if you're dealing with a UIWebView or a UIView , you can use the method isKindOfClass: to check: 如果您的代码需要知道您要处理的是UIWebView还是UIView ,则可以使用isKindOfClass:方法检查:

NSArray *myArray;
// Fill the array with a bunch of UIWebView and UIView objects here

for (id theObject in myArray)
{
    if ([theObject isKindOfClass:[UIWebView class]]) {
        // do stuff
    }
    else if ([theObject isKindOfClass:[UIView class]]) {
        // do other stuff
    }
}

Furthermore, UIWebView is a subclasses of UIView and inherits many of the same methods, making view layout the same in both cases. 此外, UIWebViewUIView的子类,并且继承了许多相同的方法,这两种情况下的视图布局都相同。

From a user interface standpoint, it's probably bad practice to have scroll views inside scroll views, so you're going to want to disable scrolling of your UIWebView objects. 从用户界面的角度来看,将滚动视图包含在滚动视图中可能是一个坏习惯,因此您将要禁用UIWebView对象的滚动。 If you are targeting iOS 5 and above you can use the following: 如果您的目标是iOS 5及更高版本,则可以使用以下命令:

webView.scrollView.scrollEnabled = NO; 
webView.scrollView.bounces = NO;

and if you're on a version of iOS lower than 5.0, you can inject the following javascript: 如果您使用的iOS版本低于5.0,则可以注入以下javascript:

<script type="text/javascript">
    touchMove = function(event) {
        event.preventDefault();
     }
</script>

To implement the javascript injection, you can use UIWebView 's stringByEvaluatingJavaScriptFromString: method inside webViewDidFinishLoad: 要实现JavaScript注入,您可以在webViewDidFinishLoad:内部使用UIWebViewstringByEvaluatingJavaScriptFromString:方法webViewDidFinishLoad:

See here for more information on UIWebView javascript injection: http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview 请参阅此处以获取有关UIWebView javascript注入的更多信息: http : //iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview


Edit: Looking at your code, it seems as thought you're sticking a UIWebView and two UIColor objects into your array. 编辑:查看您的代码,似乎就像您在将UIWebView和两个UIColor对象粘贴到数组中一样。 Therefore you need to check if you're dealing with a UIWebView or a UIColor , or you'll crash on subview.backgroundColor = [colors objectAtIndex:i]; 因此,您需要检查是否正在处理UIWebViewUIColor ,否则将在subview.backgroundColor = [colors objectAtIndex:i];上崩溃subview.backgroundColor = [colors objectAtIndex:i]; Also you set your UIWebView object's frame to CGRect frame; 同样,您将UIWebView对象的框架设置为CGRect frame; which is uninitialized. 尚未初始化。

You should populate your NSArray with your UIWebView and then two UIView objects with their background colors set: 您应该使用UIWebView填充NSArray ,然后使用背景色设置两个UIView对象:

UIWebView *webView = [[UIWebView alloc] init];
UIView *viewOne = [[UIView alloc] init];
[viewOne setBackgroundColor:[UIColor blackColor]];
UIView *viewTwo = [[UIView alloc] init];
[viewTwo setBackgroundColor:[UIColor blackColor]];

NSArray *viewArray = [NSArray arrayWithObjects:webView, viewOne, viewTwo, nil];

for (id theObject in viewArray)
{
    if ([theObject isKindOfClass:[UIWebView class]]) {
        // do stuff
    }
    else if ([theObject isKindOfClass:[UIView class]]) {
       // do other stuff
    }
}

Yes. 是。 UIWebViews descend from UIViews. UIWebViews继承自UIViews。 You may use them wherever you would insert a UIView as a subview, including within a UIScrollView parent view, or as an object to be inserted into an array. 您可以在将UIView作为子视图插入(包括在UIScrollView父视图中)或作为要插入数组的对象的任何位置使用它们。

Your scroll view is not scrolling because you didn't set the contentsize property. 您的滚动视图没有滚动,因为您没有设置contentsize属性。 First you have to find the content size appropriately, and then you have to set content size as 首先,您必须找到适当的内容大小,然后将内容大小设置为

[scrollView setContentSize:CGSize];

at the end of code. 在代码末尾。 ie; after

[self.scrollView addSubview:subview];

For a blind try, use 盲目尝试,使用

[scrollView setContentSize:CGSizeMake(1000,1000)];

this will definitely scroll the view!!! 这肯定会滚动视图!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM