6

Tôi có một câu hỏi nhanh Tôi hy vọng các bạn có thể giúp tôi trả lời. Ngay bây giờ tôi có một UIPageControl trong một kịch bản thay đổi một hình ảnh tùy thuộc vào những gì bạn đang ở trên, tuy nhiên, bây giờ bạn phải bấm vào dấu chấm để thay đổi thông qua các chấm/hình ảnh, làm thế nào tôi có thể thay đổi thông qua các hình ảnh/dấu chấm bằng cách vuốt?Xcode: Làm cách nào để thay đổi giá trị UIPageControl bằng cử chỉ vuốt?

Đây là mã của tôi cho .h tôi

#import <UIKit/UIKit.h> 

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

@end 

Đây là mã của tôi cho .m tôi

#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 

Bất kỳ trợ giúp sẽ được đánh giá rất nhiều. Cảm ơn

Trả lời

15

Bạn có thể thêm UISwipeGestureRecognizer vào chế độ xem của bạn và trong phương thức chọn của UISwipeGestureRecognizer dựa trên hướng cập nhật đối tượng UIPageControl, hoặc tăng trang hiện tại hoặc giảm dần.

Bạn có thể tham khảo mã bên dưới. Thêm swipe cử chỉ để điều khiển xem

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 cử chỉ chọn

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

Thêm một lối thoát cho UIPageControl trong file .h

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

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

@end 
+0

Awesome, nó nói "trang" là số nhận dạng không khai báo. Và nó thích hợp để thêm cái này ở đâu? Bất kỳ giúp đỡ? –

+0

Xin lỗi! quên đề cập đến, trang là đối tượng UIPageControl của bạn. –

+0

Tôi đã chỉnh sửa câu trả lời của mình, Vui lòng kiểm tra –

Các vấn đề liên quan