6

有个小问题,希望大家能帮我解答。现在我在情节提要中有一个 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

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

4

1 回答 1

16

您可以将 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
于 2013-05-06T04:18:58.290 回答