gpt4 book ai didi

ios - shouldReceiveTouch 从未被调用

转载 作者:行者123 更新时间:2023-11-29 03:21:43 24 4
gpt4 key购买 nike

我想向 R4PageViewController 添加一些 ui,例如按钮和 ListView ,但不知道这样做的正确方法,我决定尝试使用 shouldReceiveTouch,但没有成功

这是我的手势识别子类

@implementation R4SwipeGestureRecognizer

- (id)initWithPageViewController:(R4PageViewController *)pageViewController
{
self = [super initWithTarget:pageViewController action:
@selector(handleSwipeFrom:)
// nil
];
if (self) {
self.pageViewController = pageViewController;
// self.delegate = self.pageViewController;

// self.cancelsTouchesInView = YES;
// self.delaysTouchesBegan = YES;
// self.delaysTouchesEnded = YES;
}
return self;
}

//- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
//
//}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// super.delegate = self.pageViewController;
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
self.previousTouchPoint = [touch locationInView:self.view];
self.previousTouchTime = CFAbsoluteTimeGetCurrent();
self.state = (self.previousTouchPoint.x > 20) ? UIGestureRecognizerStatePossible : UIGestureRecognizerStateFailed;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];

if (self.state == UIGestureRecognizerStatePossible) {
CGFloat dx = location.x - self.previousTouchPoint.x;
CGFloat dy = location.y - self.previousTouchPoint.y;

if (ABS(dx) > ABS(dy)) {
self.state = UIGestureRecognizerStateBegan;

// load view controllers
if (dx > 0 && !self.pageViewController.previousViewContainer.viewController) {
[self.pageViewController loadPreviousViewController];
} else if (dx < 0 && !self.pageViewController.nextViewContainer.viewController){
[self.pageViewController loadNextViewController];
}

} else {
self.state = UIGestureRecognizerStateFailed;
}
} else if (self.state == UIGestureRecognizerStateBegan || self.state == UIGestureRecognizerStateChanged) {
CFAbsoluteTime time = CFAbsoluteTimeGetCurrent();

CGFloat deltaX = location.x - self.previousTouchPoint.x;
CFAbsoluteTime deltaT = time - self.previousTouchTime;

UIView *view = self.pageViewController.currentViewContainer;
view.origin = CGPointMake(view.origin.x + deltaX, view.origin.y);

// load view controllers
if (view.origin.x > 0 && !self.pageViewController.previousViewContainer.viewController) {
[self.pageViewController loadPreviousViewController];
} else if (view.origin.x < 0 && !self.pageViewController.nextViewContainer.viewController){
[self.pageViewController loadNextViewController];
}

view = self.pageViewController.previousViewContainer;
view.origin = CGPointMake(view.origin.x + deltaX * self.pageViewController.sidePagesSpaceDelayRate, view.origin.y);

view = self.pageViewController.nextViewContainer;
view.origin = CGPointMake(view.origin.x + deltaX * self.pageViewController.sidePagesSpaceDelayRate, view.origin.y);

self.velocity = deltaX / deltaT;
self.previousTouchPoint = location;
self.previousTouchTime = CFAbsoluteTimeGetCurrent();

if (self.pageViewController.currentPage == 0 && self.pageViewController.currentViewContainer.origin.x > self.pageViewController.borderPageMaxIndent) {
self.pageViewController.currentViewContainer.origin = CGPointMake(self.pageViewController.borderPageMaxIndent, self.pageViewController.currentViewContainer.origin.y);
} else if (self.pageViewController.currentPage == self.pageViewController.numberOfPages-1 && self.pageViewController.currentViewContainer.origin.x < -self.pageViewController.borderPageMaxIndent) {
self.pageViewController.currentViewContainer.origin = CGPointMake(-self.pageViewController.borderPageMaxIndent, self.pageViewController.currentViewContainer.origin.y);
}

[self.pageViewController didScrollToOffset:self.pageViewController.currentViewContainer.frame.origin.x];

self.state = UIGestureRecognizerStateChanged;
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if (self.state == UIGestureRecognizerStateBegan || self.state == UIGestureRecognizerStateChanged) {
BOOL pageChanged = NO;

UIView *curentVCView = self.pageViewController.currentViewContainer;
if (curentVCView.origin.x > curentVCView.size.width / 2 && self.pageViewController.currentPage > 0) {
[self.pageViewController shiftContainersLeft];
pageChanged = YES;
} else if (curentVCView.origin.x < -curentVCView.size.width / 2 && self.pageViewController.currentPage < self.pageViewController.numberOfPages-1) {
[self.pageViewController shiftContainersRight];
pageChanged = YES;
} else if (ABS(self.velocity) > 1000) {
if (self.velocity < 0) {
if (self.pageViewController.currentPage < self.pageViewController.numberOfPages-1) {
[self.pageViewController shiftContainersRight];
pageChanged = YES;
}
} else if (self.pageViewController.currentPage > 0) {
[self.pageViewController shiftContainersLeft];
pageChanged = YES;
}
}

[self.pageViewController animateToRestAndMakeAppearanceUpdates:^{
self.state = UIGestureRecognizerStateEnded;
if (pageChanged) {
[self.pageViewController didScrollToPage:self.pageViewController.currentPage toController:self.pageViewController.currentViewContainer.viewController];
}
}];
} else {
self.state = UIGestureRecognizerStateCancelled;
}
}

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{

CGPoint location = [touch locationInView:self.view];
NSLog(@" y location : %f", location.y);
if (location.y< 70 || location.y > 288)
{
return NO;
}
if (self.state == UIGestureRecognizerStateBegan || self.state == UIGestureRecognizerStateChanged) {
return NO;
} else {
return YES;
}
}

@end

这里是初始化函数

@implementation R4PageViewController

- (id)initWithOptions:(NSDictionary *)options
{
self.options = [options mutableCopy];
self = [super initWithNibName:@"View" bundle:nil];
if (self) {
// super.view.frame = CGRectMake(0, 70, 320, 288);
[self initializePrivateProperties];

// [self.view addSubview:[self getHeader]];

self.view.backgroundColor = [UIColor whiteColor];
self.view.layer.masksToBounds = YES;

[self loadContainerViews];

/* This guy provides swiping mechanism */
R4SwipeGestureRecognizer *r = [[R4SwipeGestureRecognizer alloc] initWithPageViewController:self];
[self.view addGestureRecognizer:r];
self.swipeGestureRecognizer = r;


r.delegate = self;

}
return self;
}

这是标题的一部分

//! R4PageViewController main class.
@interface R4PageViewController : UIViewController <UIGestureRecognizerDelegate>

@property (weak, nonatomic) id<R4PageViewControllerDataSource> dataSource;
@property (weak, nonatomic) id<R4PageViewControllerDelegate> delegate;
@property (strong, nonatomic, readonly) UIViewController *currentViewController;
@property (assign, nonatomic) NSInteger currentPage;

// Methods
- (id)initWithOptions:(NSDictionary *)options;
- (void)reloadData;

@end

我的问题:从未调用过 shouldReceiveTouch

这是一张简化想法的图片enter image description here

最佳答案

我想出了问题

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{

CGPoint location = [touch locationInView:self.view];
NSLog(@" y location : %f", location.y);
if (location.y< 70 || location.y > 288)
{
return NO;
}
if (self.state == UIGestureRecognizerStateBegan || self.state == UIGestureRecognizerStateChanged) {
return NO;
} else {
return YES;
}
}

这个函数应该放在 View Controller (R4PageViewController)实现而不是在委托(delegate)器(R4SwipeGestureRecognizer)中

关于ios - shouldReceiveTouch 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21004121/

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