你好,我已经解决了你的苹果代码问题。
只需将函数修改为:
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(0,curXLoc);
view.frame = frame;
curXLoc += (kScrollObjHeight);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake(([scrollView1 bounds].size.width),kNumImages * kScrollObjHeight)];
}
针对您的目的,请参考您的链接:
http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/
有一个类似于以下的代码:
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
要实现水平滚动,您需要将contentSize
属性的width
设置为比滚动视图的frame width
更高的值。同样,要实现垂直滚动,您需要将contentSize
属性的height
设置为比滚动视图的frame height
更高的值。
在上面的代码中,可以这样做:
scroll.contentSize = CGSizeMake(self.view.frame.size.width, numberOfViews * self.view.frame.size.height);
- (void)viewDidLoad
{
//....
UISwipeGestureRecognizer *recognizer;
//for scrolling up
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeUp)]; // calling method SwipeUp
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view] addGestureRecognizer:recognizer];
//for scrolling down
UISwipeGestureRecognizer *recognizer1;
recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeDown)]; //calling method SwipeDown
[recognizer1 setDirection:(UISwipeGestureRecognizerDirectionDown)];
[[self view] addGestureRecognizer:recognizer1];
}
//初始化并合成 IBOutlet UIImageView *bgImage;
-(IBAction)SwipeUp
{
[bgImage setImage:[UIImage imageNamed:@"yourImage1"] ];
bgImage.opaque=YES;
[self.view addSubview:bgImage];
}
-(IBAction)SwipeDown
{
[bgImage setImage:[UIImage imageNamed:@"yourImage2"] ];
bgImage.opaque=YES;
[self.view addSubview:bgImage];
}
我不确定我是否正确理解了您想要实现的内容,但基本上您可以使用setContentOffset来设置scrollView的位置。
CGPoint bottomOffset = CGPointMake(0, scrollView.contentSize.height - self.scrollView.bounds.size.height);
[scrollView setContentOffset:bottomOffset animated:YES];
有几种做法,例如:
这是创建2*2图像网格的代码片段:
//Over here adding the image names into the Array
NSMutableArray *imageArray=[[NSMutableArray alloc]init];
[Arr addObject:[UIImage imageNamed:@"image_1.png"]];
[Arr addObject:[UIImage imageNamed:@"image_2.png"]];
[Arr addObject:[UIImage imageNamed:@"image_3.png"]];
[Arr addObject:[UIImage imageNamed:@"image_4.png"]];
[Arr addObject:[UIImage imageNamed:@"image_5.png"]];
// 初始化 ScrollView 并设置框架:
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
[scrollView setBackgroundColor:[UIColor clearColor]];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
int row = 0;
int column = 0;
for(int i = 0; i < imageArray.count; i++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor clearColor];
button.frame = CGRectMake(column*160+0,row*210+10, 160, 190);
[button setImage:[imageArray objectAtIndex:i] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor clearColor]];
[button addTarget:self action:@selector(chooseCustomImageTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[view1 addSubview:button];
if (column == 1) {
column = 0;
row++;
} else {
column++;
}
}
[scrollView setContentSize:CGSizeMake(320, (row+1) * 210)];
[self.view addSubview:view1];
//这是用于点击的方法:
- (IBAction)chooseCustomImageTapped:(id)sender
{
}
现在,一旦scrollView准备好进行垂直滚动,您可以根据自己的需求进行自定义。希望这对您有所帮助。