6

我有一個快速的問題,我希望你們可以幫我回答。現在我在故事板中有一個UIPageControl,根據你所在的點是如何改變圖像的,但是,現在你必須按點來改變點/圖像,我怎樣才能通過圖像/點改變通過刷卡?Xcode:如何使用滑動手勢更改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 

任何幫助將不勝感激。 謝謝

回答

15

你可以添加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 
+0

真棒,它說該「頁面」是未聲明的標識符。在哪裏可以添加這個?任何幫助? – 2013-05-06 11:59:51

+0

對不起!忘了提及,頁面是你的UIPageControl對象。 – 2013-05-06 12:06:56

+0

我編輯了我的答案,請檢查 – 2013-05-06 12:16:01