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 に答える