0

我首先要说的是,我对 ObjectiveC 完全陌生,并且也是第一次使用界面生成器。我只是想让平移手势起作用(我也需要缩放手势来完成我真正想做的事情,但我想如果我从更简单的平移开始)。我遵循了一些教程,但它仍然无法在我的 iPhone 5 和模拟器中运行。我已经基于基于页面的应用程序开始了这件事。然后我为该视图建立了一个新视图和一个视图控制器。然后我将 Pan Gesture 从 Interface builder 拖放到视图中。视图上的用户交互已启用。

这是我的代码:

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

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

4

1 回答 1

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;
        }
于 2013-10-14T12:51:12.730 回答