Using UIGestureRecognizer
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [MoveMe.titleLabel setText:NSLocalizedString(@"Move Me Now!",@"")];
// Init UIPanGestureRecognizer * mPanGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onMoveMePan:)];
// add To MoveMe [MoveMe addGestureRecognizer:mPanGR]; } - (void) onMoveMePan:(UIPanGestureRecognizer *)gestureRecognizer { UIView * v = [gestureRecognizer view]; if (v == MoveMe ) {
//Get Locations Before Modify Any Thing ,!!! It'w may reset locations while you change other options,such like Text CGPoint translation = [gestureRecognizer translationInView:[v superview]]; CGPoint center= MoveMe.center; if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) { [MoveMe.titleLabel setText:NSLocalizedString(@"Moving!",@"")]; //Move !!! must be happen after alter other options; translation.x += center.x; translation.y += center.y; v.center = translation; } if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) { MoveMe.titleLabel.text = NSLocalizedString(@"Move End",@"");
//Move translation.x += center.x; translation.y += center.y; v.center = translation; }
// reset the Offect Vector to zero offter handled [gestureRecognizer setTranslation:CGPointZero inView:v.superview]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [MoveMe release]; [super dealloc]; }