iphone - ScrollView 中的分页

标签 iphone

_

viewdidload
{
    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [self.scrollView addSubview:subview];
        [subview release];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);

    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = colors.count;
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (!pageControlBeingUsed) {
        // Switch the indicator when more than 50% of the previous/next page is visible
        CGFloat pageWidth = self.scrollView.frame.size.width;
        int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        self.pageControl.currentPage = page;
    }
}

我正在使用此代码在 ScrollView 中以不同的颜色显示分页,为了识别,我只想用图像而不是颜色替换它。我正在使用此代码

NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:@"h1@2x"], [UIImage imageNamed:@"h2@2x"], [UIImage imageNamed:@"h2.1@2x"], nil];
    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIImageView*subview = [[UIView alloc] initWithFrame:frame];
        UIImage *imggg = [colors objectAtIndex:i];
        [subview setBackgroundColor:[UIColor colorWithPatternImage:imggg]];

        [self.scrollView addSubview:subview];
        [subview release];
    }

但是我只得到两个 View ,并且图像的大小不适合它被释放。如何使用上面的代码设置图像。请帮我解决这个问题。 提前致谢。

最佳答案

我通过将数组中图像的大小减小到 ScrollView 的大小来得到答案。我的 ScrollView 大小是 320,424,我将图像大小切片为 320,424。它工作完美。谢谢。

关于iphone - ScrollView 中的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8700737/

相关文章:

iphone - 如何学习 "Animations in COCOS2D"

ios - Facebook App Invites iOS SDK v4.0 配置和状态

ios - 当导航栏半透明 = false 时 UISearchBar 超出屏幕边界

ios - xcode 3.1.1 创建 iPhone 应用程序(3,4,5)

iphone - UIScrollView 添加 UIImageViews

iphone - 在 iPhone 上查看文件有哪些方法?

iphone - 应用程序未运行时处理推送通知

iphone - 我们如何在 Row Did Select 上更新 UITableView?

ios - Swift 3.0 字符串问题

iphone - uidatepicker 变得扭曲,需要帮助