UIScrollView在iOS 6&;中不以编程方式滚动;7两者

UIScrollView在iOS 6&;中不以编程方式滚动;7两者,ios,objective-c,uiscrollview,scroll,Ios,Objective C,Uiscrollview,Scroll,出于测试目的,我创建了一个项目,其中有两个scrollview。第一个scrollview有我手动插入的图像,第二个scrollview有按钮列表(作为菜单) 我有左右两个按钮以编程方式滚动UIScrollView 单击此按钮后,我通过设置内容大小移动scrollview myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] - 1]; CGRect page = CGRectMake(320*

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

我有左右两个按钮以编程方式滚动UIScrollView

单击此按钮后,我通过设置内容大小移动scrollview

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];
我面临的问题是只有第一个滚动视图被滚动,而不是第二个

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

我也附加在dropbox相同的项目作为代码是多一点


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

H
#导入
@界面ViewController:UIViewController
@属性(弱的,非原子的)IBUIScrollView*myScrollView;
@属性(弱,非原子)IBUILabel*myCounter;
@属性(弱的、非原子的)IBUIScrollView*firstScrollView;
-(iAction)goToLeft:(id)发送方;
-(iAction)goToRight:(id)发送方;
@结束
M
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
@综合myScrollView、myCounter、firstScrollView;
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
firstScrollView.contentSize=CGSizeMake(800200);
myCounter.text=@“0”;
}
-(无效)视图显示:(BOOL)动画{
对于(int j=0;j<2;j++){
NSString*bname=@;
int x=20;
对于(int i=0;i<8;i++){
UIButton*button=[[UIButton alloc]initWithFrame:CGRectMake(x,0,66,75)];
UIButton*右箭头按钮;
UIButton*左箭头按钮;
leftArrowButton=[[UIButton alloc]initWithFrame:CGRectMake(x-38,49,26,52)];
如果(i==2){
rightArrowButton=[[UIButton alloc]initWithFrame:CGRectMake(294,49,26,52)];
leftArrowButton=[[UIButton alloc]initWithFrame:CGRectMake(1,49,26,52)];
}else如果(i==5){
rightArrowButton=[[UIButton alloc]initWithFrame:CGRectMake(614,49,26,52)];
leftArrowButton=[[UIButton alloc]initWithFrame:CGRectMake(320,49,26,52)];
}else如果(i==7){
rightArrowButton=[[UIButton alloc]initWithFrame:CGRectMake(934,49,26,52)];
leftArrowButton=[[UIButton alloc]initWithFrame:CGRectMake(640,49,26,52)];
}否则{
rightArrowButton=[[UIButton alloc]initWithFrame:CGRectMake(x+62,49,26,52)];
leftArrowButton=[[UIButton alloc]initWithFrame:CGRectMake(x-38,49,26,52)];
}
bname=@;
如果(i==0){
bname=@“医生图标.png”;
rightArrowButton.hidden=是;
leftArrowButton.hidden=是;
}
如果(i==1){
bname=@“all-offers_icon.png”;
rightArrowButton.hidden=是;
leftArrowButton.hidden=是;
}
如果(i==2){
bname=@“hospitals_icon.png”;
rightArrowButton.hidden=否;
leftArrowButton.hidden=是;
}
如果(i==3){
bname=@“ads_icon.png”;
rightArrowButton.hidden=是;
leftArrowButton.hidden=是;
}
如果(i==4){
bname=@“alternative_icon.png”;
rightArrowButton.hidden=是;
leftArrowButton.hidden=是;
}
如果(i==5){
bname=@“pharmacy_icon.png”;
rightArrowButton.hidden=否;
leftArrowButton.hidden=否;
}
如果(i==6){
bname=@“eq_icon.png”;
rightArrowButton.hidden=是;
leftArrowButton.hidden=是;
}
如果(i==7){
bname=@“supplies_icon.png”;
rightArrowButton.hidden=是;
leftArrowButton.hidden=否;
}
UIImage*btnImage=[UIImage ImageName:bname];
[按钮设置图像:btnImage for状态:uicontrol状态正常];
[按钮设置标签:(i+1)];
[myScrollView添加子视图:按钮];
btnImage=[UIImage ImageName:@“r_arrow.png”];
[右箭头按钮设置图像:状态的btnImage:uicontrol状态正常];
[右箭头按钮设置标签:998];
[rightArrowButton addTarget:self action:@selector(goToRight:)for ControlEvents:UIControlEventTouchUpInside];
[myScrollView添加子视图:右箭头按钮];
btnImage=[UIImage ImageName:@“l_arrow.png”];
[leftArrowButton setImage:btnImage for状态:UIControlStateNormal];
[左箭头按钮设置标签:999];
[leftArrowButton addTarget:self action:@selector(goToLeft:)for ControlEvents:UIControlEventTouchUpInside];
[myScrollView添加子视图:左箭头按钮];
UIButton*button2=[[UIButton alloc]initWithFrame:CGRectMake(x,76,66,75)];
如果(i==0){
bname=@“medical_icon.png”;
}
如果(i==1){
bname=@“dental_icon.png”;
}
如果(i==2){
bname=@“beauty_icon.png”;
}
如果(i==3){
bname=@“labs_icon.png”;
}
如果(i==4){
bname=@“magazine_icon.png”;
}
如果(i==5){
bname=@“news_icon.png”;
}

如果(iHey mate),请在viewDidLoad方法中设置scrollview的contentSize

[myScrollView setContentSize:CGSizeMake(320*3,200)];

谢谢,祝你有一个幸福的一天

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


你是认真的吗?如果你看到我的问题,我已经有了…
myScrol
#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
[myScrollView setContentSize:CGSizeMake(320*3,200)];
- (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]));
}