我首先說我對於ObjectiveC是全新的,並且也是第一次使用界面生成器。 我只是想讓平底鍋的手勢工作(我將需要變焦手勢也爲我真正想做的事情,但我認爲如果我從容易的平底鍋開始)。 我跟着一些教程,但它仍然無法在我的iPhone 5和模擬器上工作。 我已經開始基於頁面的應用程序的東西。然後,我爲那個人建立了一個新的視圖和一個視圖控制器。然後,我將「界面」構建器中的Pan Gesture拖放到視圖中。視圖上的用戶交互已啓用。iOS界面識別器通過界面生成器添加
這裏是我的代碼:
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
說實話,我沒有什麼錯在這裏的想法。所以任何幫助或指針表示讚賞! 謝謝, Suse
爲什麼你使用'onGestureAction:'和'不handlePan:'? –
你試過我的代碼嗎? –
@guy Kogus:如果我改變onGestureAction到handlePan不會改變任何東西...... antionio毫克,見下文,但你的代碼還沒有做它爲我工作... – suMi