如何正确实现UIPageControl?

时间:2014-12-28 23:32:08

标签: ios objective-c uipagecontrol

我有3个与实现UIPageControl相关的小问题。我没有使用UIPageViewController,所以如果可能的话,我不想使用它。

我已经实现了一个UIPageControl,当用户在视图上向左或向右滑动时,它将移动到下一个“页面”并显示不同的图像。

以下是代码:

ViewController.h:

@property (nonatomic) NSInteger Count;

@property (strong, nonatomic) IBOutlet UIPageControl *Control;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UILabel *label;

@property (strong, nonatomic) UISwipeGestureRecognizer *swipeRight, *swipeLeft;

@property (strong, nonatomic) NSArray *array;

ViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.array = @[@"page1", @"page2", @"page3", @"page4"];

    self.swipeRight = [ [UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(Right:)];

    self.swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:self.swipeRight];

    self.swipeLeft = [ [UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(Left:)];
    self.swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:self.swipeLeft];

    self.Control.numberOfPages = 3;
    self.Control.currentPage = 0;
}

-(void)Right: (UITapGestureRecognizer *)sender
{
    self.Count = self.Count - 1;

    if (self.Count > 3)
    {
        self.Count = 1;
    }
    else if (self.Count < 1)
    {
        self.Count = 3;
    }

    if (self.Count == 1)
    {
        self.Control.currentPage = 0;
        self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
 //       self.label.text = @"this is page 1";
    }
    else if (self.Count == 2)
    {
        self.Control.currentPage = 1;
        self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
       // self.label.text = @"this is page 2";
    }
    else if (self.Count == 3)
    {
        self.Control.currentPage = 2;
        self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
//        self.label.text = @"this is page 3";
    }
}

-(void)Left: (UITapGestureRecognizer *)sender
{
    self.Count = self.Count + 1;

    if (self.Count > 3)
    {
        self.Count = 1;
    }
    else if (self.Count < 1)
    {
        self.Count = 3;
    }

    if (self.Count == 1)
    {
        self.Control.currentPage = 0;
        self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
        //self.label.text = @"this is page 1";
    }
    else if (self.Count == 2)
    {
        self.Control.currentPage = 1;
        self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
        //self.label.text = @"this is page 2";
    }
    else if (self.Count == 3)
    {
        self.Control.currentPage = 2;
        self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
       // self.label.text = @"this is page 3";
    }
}

第一个问题:如何让视图在发布时显示索引0处的第一张图片?

现在,如果您最初启动应用程序,即使页面控件将当前视图显示为使用小点突出显示,图像在屏幕上仍为空白。您必须向左滑动才能显示索引0中的第一个图像。

第二个问题:如何向左或向右滑动时添加滚动过渡样式动画?

现在,如果向左或向右滑动,图像过渡是即时的,并且您看不到滑动动画,其中一个图像将另一个图像推出视图。

第三个问题:如何使滑动手势识别不是整个视图区域,但只有在视图的上半部分滑动时才可以滑动?

随意下载项目代码,因为它只是我正在使用的模板: project code

由于

0 个答案:

没有答案