ios 在 iPhone 中使用页面控制滑动图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13061616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:56:37  来源:igfitidea点击:

Swiping Images with Page Control in Iphone

iphoneiosuiscrollviewscrolluipagecontrol

提问by lakesh

I am trying to make practice app where i can scroll images with page control. I am able to scroll images and able to include the page control. But the problem i face is i am not able to interlink the two. Meaning to say when I scroll the images, the page control is not affected and when i change the page control, the scrolling of the images is unaffected.

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

I have referred to this: http://www.iosdevnotes.com/2011/03/uiscrollview-paging/for the scrolling with page control.

我已经提到了这个:http: //www.iosdevnotes.com/2011/03/uiscrollview-paging/用于滚动页面控制。

Viewcontroller.h

视图控制器.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

视图控制器.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

Need some guidance on this... Thanks..

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

采纳答案by vignesh kumar

Check whether you have set the delegate of the scroll view to the ViewController

检查您是否已将滚动视图的委托设置为 ViewController

And you have just set the pageControlBeingUsed to YES or NO and you have not used it anywhere in the ViewController

您刚刚将 pageControlBeingUsed 设置为 YES 或 NO,并且您还没有在 ViewController 的任何地方使用它

回答by Manish Agrawal

I learned page control and scrollView from this tutorial, its very clearly written, hope it helps you http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content

我从本教程中学习了页面控制和滚动视图,写得很清楚,希望对您有所帮助http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content