我想監視上,下,左,右滑動手勢從外部類(即與方法不在我的視圖控制器中)。我已經設法使用外部類和屬性來判斷哪個方向被推動,但我現在想要在檢測到滑動時在視圖控制器內運行一個方法(它將接受哪個方向被滑動,並且按指示行動)。從非視圖控制器類監視手勢
我不確定如何讓一個類中的方法在另一個類型中檢測到滑動時運行。目前,我的SwipeDetector類的設置如下所示,並且我希望這些kDirectionKey常量被引入視圖控制器類中的一個方法中,並且每次刷新時都會觸發該方法。這是我應該使用觀察員的東西嗎?我從來沒有用過它們,看起來有點令人生畏。
@synthesize up = _up;
@synthesize down = _down;
@synthesize left = _left;
@synthesize right = _right;
@synthesize swipedDirection = _swipedDirection;
- (void)recogniseDirectionSwipes
{
_up = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(upSwipeDetected)];
_down = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(downSwipeDetected)];
_left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeDetected)];
_right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeDetected)];
_up.direction = UISwipeGestureRecognizerDirectionUp;
_down.direction = UISwipeGestureRecognizerDirectionDown;
_left.direction = UISwipeGestureRecognizerDirectionLeft;
_right.direction = UISwipeGestureRecognizerDirectionRight;
}
- (void)upSwipeDetected
{
NSLog(@"Direction swipe sniffed out, and that direction was up!");
_swipedDirection = kDirectionKeyUp;
}
- (void)downSwipeDetected
{
NSLog(@"Direction swipe sniffed out, and that direction was down!");
_swipedDirection = kDirectionKeyDown;
}
- (void)leftSwipeDetected
{
NSLog(@"Direction swipe sniffed out, and that direction was left!");
_swipedDirection = kDirectionKeyLeft;
}
- (void)rightSwipeDetected
{
NSLog(@"Direction swipe sniffed out, and that direction was right!");
_swipedDirection = kDirectionKeyRight;
}
@end