iphone - 在 Iphone 中使用页面控件滑动图像

标签 iphone ios uiscrollview scroll uipagecontrol

我正在尝试制作可以使用页面控件滚动图像的练习应用程序。我能够滚动图像并能够包含页面控件。但我面临的问题是我无法将两者联系起来。意思是说当我滚动图像时,页面控件不受影响,当我更改页面控件时,图像的滚动不受影响。

我提到过这个:http://www.iosdevnotes.com/2011/03/uiscrollview-paging/用于使用页面控件滚动。

View Controller .h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>{
    UIScrollView *scrollView;
    UIPageControl *pageControl;

    BOOL pageControlBeingUsed;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;

- (IBAction)changePage;

@end

Viewcontroller.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize scrollView,pageControl;

- (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;
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1.jpeg"],[UIImage imageNamed:@"2.jpeg"],[UIImage imageNamed:@"3.jpeg" ], nil];

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

    for (int i = 0; i < images.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        UIImageView* imgView = [[UIImageView alloc] init];
        imgView.image = [images objectAtIndex:i];
        imgView.frame = frame;
        [scrollView addSubview:imgView];
    }

    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = images.count;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.scrollView = nil;
    self.pageControl = nil;
}

- (IBAction)changePage{
        // update the scroll view to the appropriate page
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        [self.scrollView scrollRectToVisible:frame animated:YES];
        pageControlBeingUsed = YES;
}

@end

需要一些指导......谢谢..

最佳答案

通过这个教程学习了page control和scrollView,写的很清楚,希望对你有帮助http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content

关于iphone - 在 Iphone 中使用页面控件滑动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13061616/

相关文章:

iOS 崩溃报告 : EXC_CRASH (SIGABRT) - stringByAppendingString

ios - 如何找到 ScrollView 内容偏移 Y 点以使所选文本可见。

iphone - 返回方法对象 c 的释放对象

ios - 对应真实的iDevice和iOS模拟器

iphone - Core Graphics 画线带轮廓

iphone - 网站导致移动Safari崩溃(可复制)

iOS AdMob 奖励视频

ios - 我尝试在两个行星之间创建路径,没有编译错误,但是我不明白为什么模拟器上什么也没有出现

ios - 滚动 UITableView 时如何创建 NSLayoutConstraint

ios - UIScrollView初始偏移量