以编程方式创建UI Pan Gesture

时间:2015-04-20 11:06:13

标签: ios objective-c uipangesturerecognizer

我正在使用第三方库,当用户平移视图时使用它(使用UIPanGestureRecognizer

但是,我想在按钮点击时调用此方法。因此,我如何在代码中对panning进行硬编码,因此我无需编辑3rd party library

换句话说,我如何以编程方式创建UIPanGesture

我希望我的问题很清楚,有人可以帮助我。

来自第三方库的

代码:

- (void)vPannedHorizontally:(UIPanGestureRecognizer*)gesture

{

   ...

 }

按钮Click ViewCOntroller中的点击事件:

-(IBAction)clickedNow:(id)sender{



 ThirdPartyLib *tpl =[[ThirdPartyLib alloc]init];

[tpl vPannedHorizontally: ??????]; // I need to hardcode a UIPanGesture so it works.

}

更新

#import "ContentViewController.h"
#import "PaperFoldView.h"

@interface ContentViewController ()
@property (nonatomic, retain) PaperFoldView *paperFoldView;

@end
@implementation ContentViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
                                       initWithTitle:@"Flip"
                                       style:UIBarButtonItemStyleBordered
                                       target:self
                                       action:@selector(flipView:)];
        self.navigationItem.leftBarButtonItem = flipButton;
    }
    return self;
}

-(void)flipView:(id)sender{
    NSLog(@"Hi");
    [self.paperFoldView setPaperFoldState:PaperFoldStateLeftUnfolded animated:YES];
}

2 个答案:

答案 0 :(得分:0)

每个手势识别器都会获得一个回调方法,当它们发生某些事件时,它们会响应(状态改变,......)。所以你的第三方图书馆必须有一个回调。通过回调,我的意思是[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panCallback:)],在这种情况下- (void)panCallback:(UIPanGestureRecognizer *)sender将被调用实例化识别器的任何人。

您要做的是找出第三方库中回调的名称。然后创建一个UIPanGestureRecognizer,您可以在其上设置所有必需的内容,例如翻译。

UIPanGestureRecognizer *reco = UIPanGestureRecnogizer.new;
[reco setTranslation:CGPointMake(20, 20) inView:self.view]; //Or whatever you want
//reco.state = UIGestureRecognizerStateChanged; //If you want this too you'll have to subclass it

//Now call the callback and pass your gesture recognizer
[my3rdLibraryClassInstance performSelector:@selector(panCallback:) withObject:reco];

希望这可以解决一些问题。

编辑:至于折叠库,我注意到有一些方法可以用来手工折叠。

  1. 通过设置属性paperFoldInitialPanDirection
  2. 来设置方向
  3. 在折叠视图上调用animateWithContentOffset:(CGPoint)point panned:(BOOL)panned并相应地设置点
  4. 玩一下参数。我不确定它会起作用,但我认为应该(我只是快速瞥见了实施)。报告尝试时会发生什么。

    编辑2:最终解决方案

    如果要打开视图,库已经提供了执行此操作的方法。只需拨打

    即可
    [self.paperFoldView setPaperFoldState:PaperFoldStateLeftUnfolded animated:YES];
    

    这是通过包含的演示项目进行测试的。而且你已经完成了。

答案 1 :(得分:0)

我认为你不需要为此模拟手势。

调用PaperFoldView上的方法手动折叠/展开。

-(IBAction)open:(id)sender
{
   //For some reason the timerStepDuration is set to 0 by default so you need to set this otherwise there won't be an animation.
   self.paperFoldView.timerStepDuration = 0.02;
   [self.paperFoldView setPaperFoldState:PaperFoldStateLeftUnfolded];
}
-(IBAction)close:(id)sender
{
   //For some reason the timerStepDuration is set to 0 by default so you need to set this otherwise there won't be an animation.
   self.paperFoldView.timerStepDuration = 0.02;
   [self.paperFoldView setPaperFoldState:PaperFoldStateDefault];
}