• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIScrollView 在 iOS 6 和 7 中都没有以编程方式滚动

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:08:28 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

出于测试目的,我创建了有两个 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];

我面临的问题是只有第一个 ScrollView 被滚动,而不是第二个。

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

我还在 Dropbox 中附加了相同的项目,因为代码不多。

Project at dropbox


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

.h

#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)goToLeftid)sender;
- (IBAction)goToRightid)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) viewDidAppearBOOL)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 setTagi+1)];
            [myScrollView addSubview:button];


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

            btnImage = [UIImage imageNamed"l_arrow.png"];
            [leftArrowButton setImage:btnImage forState:UIControlStateNormal];
            [leftArrowButton setTag:999];
            [leftArrowButton addTarget:self actionselector(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 setTagi+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)goToLeftid)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)goToRightid)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



Best Answer-推荐答案


我已经下载了你的代码并修改了下面的代码,你的“page.origin.y is out of the contentSize.height”

- (IBAction)goToLeftid)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)goToRightid)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]));
}

关于ios - UIScrollView 在 iOS 6 和 7 中都没有以编程方式滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20513489/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap