gpt4 book ai didi

objective-c - Xcode:如何使用滑动手势更改 UIPageControl 值?

转载 作者:太空狗 更新时间:2023-10-30 03:40:14 25 4
gpt4 key购买 nike

我有一个简短的问题,希望你们能帮我回答。现在我在 Storyboard 中有一个 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 添加到您的 View 中,并在 UISwipeGestureRecognizer 的选择器方法中根据方向更新 UIPageControl 对象,增加或减少当前页面。

您可以引用下面的代码。向 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];

滑动手势选择器

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

关于objective-c - Xcode:如何使用滑动手势更改 UIPageControl 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16391048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com