ios - UIScrollView 在 iOS 6 和 7 中都没有以编程方式滚动
<p><p>出于测试目的,我创建了有两个 ScrollView 的项目。第一个 ScrollView 有我手动插入的图像,在第二个 ScrollView 中我有按钮列表(作为菜单)。</p>
<p>我有左右按钮以编程方式滚动 UIScrollView。</p>
<p>单击此按钮后,我通过设置内容大小来移动 ScrollView 。</p>
<pre><code>myCounter.text = - 1];
CGRect page = CGRectMake(320*( ), 230, 320, 150);
;
CGRect page2 = CGRectMake(320*( ), 20, 320, 200);
;
</code></pre>
<p>我面临的问题是只有第一个 ScrollView 被滚动,而不是第二个。</p>
<p>我真的很困惑为什么会这样。</p>
<p>我还在 Dropbox 中附加了相同的项目,因为代码不多。</p>
<h2> <a href="https://www.dropbox.com/s/lf91221thwvy3i3/ScrollTestImages.zip" rel="noreferrer noopener nofollow">Project at dropbox</a> </h2>
<hr/>
<p>如果有人不想下载项目的完整代码</p>
<h2>.h</h2>
<pre><code>#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
</code></pre>
<h2>.m</h2>
<pre><code>#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myScrollView, myCounter, firstScrollView;
- (void)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 = [ initWithFrame:CGRectMake(x, 0, 66, 75)];
UIButton *rightArrowButton;
UIButton *leftArrowButton;
leftArrowButton = [ initWithFrame:CGRectMake(x-38, 49, 26, 52)];
if (i==2) {
rightArrowButton = [ initWithFrame:CGRectMake(294, 49, 26, 52)];
leftArrowButton = [ initWithFrame:CGRectMake(1, 49, 26, 52)];
} else if (i==5) {
rightArrowButton = [ initWithFrame:CGRectMake(614, 49, 26, 52)];
leftArrowButton = [ initWithFrame:CGRectMake(320, 49, 26, 52)];
} else if (i==7) {
rightArrowButton = [ initWithFrame:CGRectMake(934, 49, 26, 52)];
leftArrowButton = [ initWithFrame:CGRectMake(640, 49, 26, 52)];
} else {
rightArrowButton = [ initWithFrame:CGRectMake(x+62, 49, 26, 52)];
leftArrowButton = [ 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 = ;
;
;
;
btnImage = ;
;
;
;
;
btnImage = ;
;
;
;
;
UIButton *button2 = [ 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 = ;
;
;
;
}
x += button.frame.size.width + 40;
}
}
// myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(320*3, 150);
myScrollView.backgroundColor = ;
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
- (IBAction)goToLeft:(id)sender {
myCounter.text = - 1];
CGRect page = CGRectMake(320*( ), 230, 320, 150);
;
CGRect page2 = CGRectMake(320*( ), 20, 320, 200);
;
NSLog(@"myCounter.text==%d", 320*());
}
- (IBAction)goToRight:(id)sender {
myCounter.text = + 1];
CGRect page = CGRectMake(320*( ), 230, 320, 150);
;
CGRect page2 = CGRectMake(320*( ), 20, 320, 200);
;
NSLog(@"myCounter.text==%d", 320*());
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我已经下载了你的代码并修改了下面的代码,你的“page.origin.y is out of the contentSize.height”</p>
<pre><code>- (IBAction)goToLeft:(id)sender {
myCounter.text = - 1];
CGRect page = CGRectMake(320*( ), 0, 320, 150);
;
CGRect page2 = CGRectMake(320*( ), 0, 320, 200);
;
NSLog(@"myCounter.text==%d", 320*());
}
- (IBAction)goToRight:(id)sender {
myCounter.text = + 1];
CGRect page = CGRectMake(320*( ), 0, 320, 150);
;
CGRect page2 = CGRectMake(320*( ), 0, 320, 200);
;
NSLog(@"myCounter.text==%d", 320*());
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - UIScrollView 在 iOS 6 和 7 中都没有以编程方式滚动,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20513489/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20513489/
</a>
</p>
页:
[1]