objective-c – Xcode:如何使用滑动手势更改UIPageControl值?

我有一个简单的问题,我希望你们能帮助我回答.现在我在故事板中有一个UIPageControl根据你所在的点改变图像,但是,到现在你必须按下点来改变点/图像,我怎样才能改变图像/点通过刷?

这是我的.h的代码

#import <UIKit/UIKit.h>

@interface PageViewController : UIViewController
@property (strong,nonatomic) IBOutlet UIImageView *dssview;
- (IBAction)changephoto:(UIPageControl *)sender;

@end

这是我的.m的代码

#import "PageViewController.h"

@interface PageViewController ()
@end

@implementation PageViewController

- (id)initWithNibName:(Nsstring *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
    // Custom initialization
  }
  return self;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // dispose of any resources that can be recreated.
}

- (IBAction)changephoto:(UIPageControl *)sender {
  _dssview.image = [UIImage imageNamed:
                    [Nsstring stringWithFormat:@"%d.jpg",sender.currentPage+1]];
}
@end

任何帮助将不胜感激.
谢谢

解决方法

您可以根据更新UIPageControl对象的方向将UISwipeGestureRecognizer添加到视图和UISwipeGestureRecognizer的选择器方法中,或者递增当前页面或递减.

您可以参考以下代码.
将滑动手势添加到视图控制器

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

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];

滑动手势选择器

- (void)swipe:(UISwipeGestureRecognizer *)swipeRecogniser
{
    if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionLeft)
    {
         self.pageControl.currentPage -=1;
    }
    else if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionRight)
    {
         self.pageControl.currentPage +=1;
    }
    _dssview.image = [UIImage imageNamed:
                [Nsstring stringWithFormat:@"%d.jpg",self.pageControl.currentPage]];
}

Add an outlet to the UIPageControl in the .h file

@interface PageViewController : UIViewController
@property (strong,nonatomic) IBOutlet UIImageView *dssview;
@property (strong,nonatomic) IBOutlet UIPageControl *pageControl;

 - (IBAction)changephoto:(UIPageControl *)sender;

@end

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...