Xcode中:如何更改与轻扫手势UIPageControl价值?(Xcode: How do I c

2019-09-02 01:42发布

我有一个快速的问题,我希望你们能帮助我解答。 现在我有一个故事板根据什么点你,然而,截至目前,你必须按下点通过点/图像改变时改变的图像,一个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

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

Answer 1:

您可以添加UISwipeGestureRecognizer到您的视图,并基于方向UISwipeGestureRecognizer的选择方法更新UIPageControl对象,无论是增加当前页面或递减。

您可以参考下面的代码。 添加滑动手势视图控制器

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]];
}

出口添加到UIPageControl在.h文件

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

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

@end


文章来源: Xcode: How do I change UIPageControl value with swipe gesture?