iOS pan Recogniser通过界面构建​​器添加

时间:2013-10-14 12:46:00

标签: iphone ios objective-c xcode interface-builder

我首先说我是objectiveC的新手,并且我第一次使用界面构建器。 我只想让平移手势工作(我还需要缩放手势,我还想要做什么,但我想如果我从更简单的平底锅开始)。 我遵循了一些教程,但它仍然无法在我的iPhone 5和模拟器中工作。 我已经基于基于页面的应用程序启动了这个东西。然后,我为该视图建立了一个新视图和一个视图控制器。然后我将Pan Gesture从Interface构建器拖放到视图中。已启用视图上的用户交互。

这是我的代码:

uACropViewController.h

 #import <UIKit/UIKit.h>

@interface uACropViewController : UIViewController
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer;
@property (weak, nonatomic) IBOutlet UIImageView *CropImage;
@property (strong, nonatomic) IBOutlet UIPanGestureRecognizer *onGestureAction;

@end

uACropViewController.m

#import "uACropViewController.h"

@interface uACropViewController ()

@end

@implementation uACropViewController
@synthesize CropImage;
static CGPoint originalPoint;

- (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.
    //for resetting the center of the image, when the pan gesture is ended.
    originalPoint=CropImage.center;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)onGestureAction:(id)sender{
    if ([sender isKindOfClass:[UIPanGestureRecognizer class]]) {
        //is a pan gesture
        UIPanGestureRecognizer *recognizer=(UIPanGestureRecognizer *)sender;

        if (recognizer.state==UIGestureRecognizerStateBegan||recognizer.state==UIGestureRecognizerStateChanged) {
            CGPoint nextPoint= [recognizer translationInView:self.view];
            CGPoint currentPoint=CropImage.center;
            currentPoint.x +=nextPoint.x;
            currentPoint.y +=nextPoint.y;

            CropImage.center=currentPoint;
            [recognizer setTranslation:CGPointZero inView:self.view];

        }
        if (recognizer.state==UIGestureRecognizerStateEnded) {
            CropImage.center=originalPoint;
            [recognizer setTranslation:CGPointZero inView:self.view];
        }

        return;
    }

}

@end

说实话,我不知道这里出了什么问题。所以任何帮助或指针都表示赞赏! 谢谢, 粟色

1 个答案:

答案 0 :(得分:0)

IBOutlet是视图中对该对象的引用,IBAction是在该变量上触发事件时调用的动作。

例如,您可以使用UIButton IBOutlet来更改UIButton的大小属性,使用UIButton IBAction来识别按下按钮的时间。

在你的情况下,onGestureAction是一个指向该手势识别器的变量,而hanldePan:是动作响应者。

尝试在此处添加方法:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
            if (recognizer.state==UIGestureRecognizerStateBegan||recognizer.state==UIGestureRecognizerStateChanged) {
                CGPoint nextPoint= [recognizer translationInView:self.view];
                CGPoint currentPoint=CropImage.center;
                currentPoint.x +=nextPoint.x;
                currentPoint.y +=nextPoint.y;

                CropImage.center=currentPoint;
                [recognizer setTranslation:CGPointZero inView:self.view];

            }
            if (recognizer.state==UIGestureRecognizerStateEnded) {
                CropImage.center=originalPoint;
                [recognizer setTranslation:CGPointZero inView:self.view];
            }

            return;
        }