UIScrollView 在 iOS 6 & 中不以编程方式滚动7个 [英] UIScrollView not scrolling programmatically in iOS 6 & 7 both

查看:32
本文介绍了UIScrollView 在 iOS 6 & 中不以编程方式滚动7个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于测试目的,我创建了具有两个滚动视图的项目.第一个滚动视图有我手动插入的图像,在第二个滚动视图中我有按钮列表(作为菜单).

For testing purpose I have create project where I have two scrollview. First scrollview have images that I inserted manually and in second scrollview I have list of buttons (as menu).

我有左右按钮可以以编程方式滚动 UIScrollView.

I have button as right and left to scroll UIScrollView programmatically.

单击此按钮后,我通过设置内容大小来移动滚动视图.

After clicking this button I was moving scrollview by setting content size.

myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] - 1];

CGRect page = CGRectMake(320*([myCounter.text intValue] ), 230, 320, 150);

[myScrollView scrollRectToVisible:page animated:YES];

CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 200);

[firstScrollView scrollRectToVisible:page2 animated:YES];

我面临的问题是只有第一个滚动视图被滚动,而第二个没有.

The problem I am facing is only first scroll view is getting scrolled but not the second one.

我真的很困惑为什么会这样.

I am really confused why this is happening.

我也在 dropbox 中附加了相同的项目,因为代码不多.

I am also attaching same project at dropbox as code is little more.

如果有人不想下载项目的完整代码

Full Code if some one don't want to download project

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (weak, nonatomic) IBOutlet UILabel *myCounter;
@property (weak, nonatomic) IBOutlet UIScrollView *firstScrollView;

- (IBAction)goToLeft:(id)sender;
- (IBAction)goToRight:(id)sender;
@end

.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize myScrollView, myCounter, firstScrollView;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    firstScrollView.contentSize = CGSizeMake(800, 200);

    myCounter.text = @"0";

}

- (void) viewDidAppear:(BOOL)animated {
    for (int j = 0; j < 2; j++) {
        NSString *bname = @"";
        int x = 20;
        for (int i = 0; i < 8; i++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 66, 75)];
            UIButton *rightArrowButton;


            UIButton *leftArrowButton;

            leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(x-38, 49, 26, 52)];

            if (i==2) {
                rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(294, 49, 26, 52)];
                leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(1, 49, 26, 52)];
            } else if (i==5) {
                rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(614, 49, 26, 52)];
                leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(320, 49, 26, 52)];
            } else if (i==7) {
                rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(934, 49, 26, 52)];
                leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(640, 49, 26, 52)];
            } else {
                rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(x+62, 49, 26, 52)];
                leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(x-38, 49, 26, 52)];
            }


            bname = @"";
            if (i==0) {
                bname = @"doctors_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }
            if (i==1) {
                bname = @"all-offers_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }
            if (i==2) {
                bname = @"hospitals_icon.png";
                rightArrowButton.hidden = NO;
                leftArrowButton.hidden = YES;
            }

            if (i==3) {
                bname = @"ads_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }
            if (i==4) {
                bname = @"alternative_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }
            if (i==5) {
                bname = @"pharmacy_icon.png";
                rightArrowButton.hidden = NO;
                leftArrowButton.hidden = NO;
            }

            if (i==6) {
                bname = @"equ_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }

            if (i==7) {
                bname = @"supplies_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = NO;
            }

            UIImage *btnImage = [UIImage imageNamed:bname];
            [button setImage:btnImage forState:UIControlStateNormal];
            [button setTag:(i+1)];
            [myScrollView addSubview:button];


            btnImage = [UIImage imageNamed:@"r_arrow.png"];
            [rightArrowButton setImage:btnImage forState:UIControlStateNormal];
            [rightArrowButton setTag:998];
            [rightArrowButton addTarget:self action:@selector(goToRight:) forControlEvents:UIControlEventTouchUpInside];
            [myScrollView addSubview:rightArrowButton];

            btnImage = [UIImage imageNamed:@"l_arrow.png"];
            [leftArrowButton setImage:btnImage forState:UIControlStateNormal];
            [leftArrowButton setTag:999];
            [leftArrowButton addTarget:self action:@selector(goToLeft:) forControlEvents:UIControlEventTouchUpInside];
            [myScrollView addSubview:leftArrowButton];


            UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(x, 76, 66, 75)];

            if (i==0) {
                bname = @"medical_icon.png";
            }
            if (i==1) {
                bname = @"dental_icon.png";
            }
            if (i==2) {
                bname = @"beauty_icon.png";
            }
            if (i==3) {
                bname = @"labs_icon.png";
            }
            if (i==4) {
                bname = @"magazine_icon.png";
            }
            if (i==5) {
                bname = @"news_icon.png";
            }

            if (i<=5) {

                btnImage = [UIImage imageNamed:bname];
                [button2 setImage:btnImage forState:UIControlStateNormal];
                [button2 setTag:(i+8+1)];
                [myScrollView addSubview:button2];

            }

            x += button.frame.size.width + 40;

        }
    }
    //    myScrollView.delegate = self;
    myScrollView.contentSize = CGSizeMake(320*3, 150);
    myScrollView.backgroundColor = [UIColor clearColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)goToLeft:(id)sender {

    myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] - 1];

    CGRect page = CGRectMake(320*([myCounter.text intValue] ), 230, 320, 150);

    [myScrollView scrollRectToVisible:page animated:YES];

    CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 200);

    [firstScrollView scrollRectToVisible:page2 animated:YES];


    NSLog(@"myCounter.text==%d", 320*([myCounter.text intValue]));

}

- (IBAction)goToRight:(id)sender {
    myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] + 1];

    CGRect page = CGRectMake(320*([myCounter.text intValue] ), 230, 320, 150);
    [myScrollView scrollRectToVisible:page animated:YES];

    CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 200);
    [firstScrollView scrollRectToVisible:page2 animated:YES];
    NSLog(@"myCounter.text==%d", 320*([myCounter.text intValue]));
}
@end

推荐答案

我已经下载了你的代码并修改了下面的代码,你的page.origin.y超出了contentSize.height"

I have downloaded your code and modify the code below, your "page.origin.y is out of the contentSize.height"

- (IBAction)goToLeft:(id)sender {

    myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] - 1];

    CGRect page = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 150);

    [myScrollView scrollRectToVisible:page animated:YES];

    CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 200);

    [firstScrollView scrollRectToVisible:page2 animated:YES];


    NSLog(@"myCounter.text==%d", 320*([myCounter.text intValue]));

}

- (IBAction)goToRight:(id)sender {
    myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] + 1];

    CGRect page = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 150);
    [myScrollView scrollRectToVisible:page animated:YES];

    CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 200);
    [firstScrollView scrollRectToVisible:page2 animated:YES];
    NSLog(@"myCounter.text==%d", 320*([myCounter.text intValue]));
}

这篇关于UIScrollView 在 iOS 6 &amp; 中不以编程方式滚动7个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆