我首先要说的是,我对 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
老实说,我不知道这里出了什么问题。所以任何帮助或指针表示赞赏!谢谢,苏塞