Xcode:如何使用滑动手势更改UIPageControl的值? [英] Xcode: How do I change UIPageControl value with swipe gesture?

查看:124
本文介绍了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

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

解决方案

您可以将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]];
}

在.h文件中的UIPageControl中添加插座

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

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

@end

I have a quick question I was hoping you guys could help me answer. Right now I have a UIPageControl in a storyboard which changes an image depending on what dot you are on, however, as of now you have to press on the dot to change through the dots/images, how can I change through the images/dots by swiping?

Here is my code for my .h

#import <UIKit/UIKit.h>

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

@end

Here is my code for my .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

Any help would be greatly appreciated. Thanks

解决方案

You can add UISwipeGestureRecognizer to your view and in the selector method of UISwipeGestureRecognizer based on the direction update the UIPageControl object, either increment the current page or decrement.

You can refer the below code. Adding swipe gesture to the view controller

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

Swipe gesture selector

- (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

这篇关于Xcode:如何使用滑动手势更改UIPageControl的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆