標籤:
#import "ViewController.h"@interface ViewController ()/** 建立一個UIView */@property(nonatomic , weak) UIView * gestureView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //建立一個uiview UIView * tempView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 100, 100)]; //設定背景顏色 tempView.backgroundColor = [UIColor orangeColor]; self.gestureView=tempView; //建立手勢 UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)]; //綁定手勢 [self.gestureView addGestureRecognizer:pan]; [self.view addSubview:self.gestureView];}- (void)panGes:(UIPanGestureRecognizer *)pan{ CGPoint tranP = [pan translationInView:self.gestureView]; self.gestureView.transform = CGAffineTransformTranslate(self.gestureView.transform, tranP.x, tranP.y); [pan setTranslation:CGPointZero inView:self.gestureView];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
xcode - 移動手勢