如何将pan手势事件从子视图传递到父视图

时间:2013-05-27 03:40:50

标签: ios subview uipangesturerecognizer

enter image description here

大家好,我怎样才能将来自imageView的平移手势事件传递给他的父视图(即高亮显示)?

1 个答案:

答案 0 :(得分:1)

您可以使用下面的代码。它假定您在故事板中进行了设置:具有视图的视图控制器,以及已连接了平移手势识别器的子视图,并且IBOutletsIBAction都已正确连接,并且视图控制器是手势识别器的delegate 您的想法是在第一次触摸时存储子视图的中心。当您平移子视图时,手势识别器会为您提供从此位置移动的金额。试一试吧。

#import "ViewController.h"

@interface ViewController (){
    CGPoint subViewCenter;
}

@property (weak) IBOutlet UIView * subView;
@property (weak) IBOutlet UIPanGestureRecognizer *myPanGestureRecognizer;

@end

@implementation ViewController

@synthesize subView;
@synthesize myPanGestureRecognizer;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    subViewCenter = self.subView.center;
}

- (IBAction)panAction:(UIPanGestureRecognizer *)sender {
    CGPoint move = [sender translationInView:self.view];
    CGPoint newCenter = subViewCenter;
    newCenter.x += move.x; newCenter.y += move.y;
    subView.center = newCenter;
}

@end