如何使用UIPanGestureRecognizer移动对象? iPhone / iPad版 [英] How to use UIPanGestureRecognizer to move object? iPhone/iPad
问题描述
有几个 UIPanGestureRecognizer
类的例子。例如,我已阅读,我认为这就是我要找的。它帮助我提出了以下解决方案:
- (IBAction)someMethod {
UIPanGestureRecognizer * panRecognizer = [[ UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move :)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[ViewMain addGestureRecognizer:panRecognizer];
[panRecognizer发布];
}
- (无效)move:(UIPanGestureRecognizer *)发件人{
[self.view bringSubviewToFront:sender.view];
CGPoint translatedPoint = [sender translationInView:sender.view.superview];
if(sender.state == UIGestureRecognizerStateBegan){
firstX = sender.view.center.x;
firstY = sender.view.center.y;
}
translatedPoint = CGPointMake(sender.view.center.x + translatedPoint.x,sender.view.center.y + translatedPoint.y);
[sender.view setCenter:translatedPoint];
[发件人setTranslation:CGPointZero inView:sender.view];
if(sender.state == UIGestureRecognizerStateEnded){
CGFloat velocityX =(0.2 * [sender velocityInView:self.view] .x);
CGFloat velocityY =(0.2 * [sender velocityInView:self.view] .y);
CGFloat finalX = translatedPoint.x + velocityX;
CGFloat finalY = translatedPoint.y + velocityY; // translatedPoint.y +(.35 * [(UIPanGestureRecognizer *)sender velocityInView:self.view] .y);
if(finalX< 0){
finalX = 0;
} else if(finalX> self.view.frame.size.width){
finalX = self.view.frame.size.width;
}
if(finalY< 50){//以避免状态栏
finalY = 50;
}否则if(finalY> self.view.frame.size.height){
finalY = self.view.frame.size.height;
}
CGFloat animationDuration =(ABS(velocityX)*。0002)+。2;
NSLog(@持续时间为:%f,animationDuration);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidFinish)];
[[发送者视图] setCenter:CGPointMake(finalX,finalY)];
[UIView commitAnimations];
}
}
There are several examples of the UIPanGestureRecognizer
class. For example I have read this and I am still not able to use it...
On the nib file that I am working on I have a UIView
(white rectangle on image) that I wish to drag with that class:
and in my .m file I have placed:
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
{
NSLog(@"Test to see if this method gets executed");
}
and that method does not get executed when I drag the mouse across the UIView
. I have also tried placing:
- (void)pan:(UIPanGestureRecognizer *)gesture
{
NSLog(@"testing");
}
And that method does not get executed either. Maybe I am wrong but I think this methods should work like the - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
method where I just have to place that method and it will get called whenever there are touches.
What am I doing wrong? Maybe do I have to draw a connection to that method? If so how can I do that?
I found the tutorial Working with UIGestureRecognizers, and I think that is what I am looking for. It helped me come up with the following solution:
-(IBAction) someMethod {
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[ViewMain addGestureRecognizer:panRecognizer];
[panRecognizer release];
}
-(void)move:(UIPanGestureRecognizer*)sender {
[self.view bringSubviewToFront:sender.view];
CGPoint translatedPoint = [sender translationInView:sender.view.superview];
if (sender.state == UIGestureRecognizerStateBegan) {
firstX = sender.view.center.x;
firstY = sender.view.center.y;
}
translatedPoint = CGPointMake(sender.view.center.x+translatedPoint.x, sender.view.center.y+translatedPoint.y);
[sender.view setCenter:translatedPoint];
[sender setTranslation:CGPointZero inView:sender.view];
if (sender.state == UIGestureRecognizerStateEnded) {
CGFloat velocityX = (0.2*[sender velocityInView:self.view].x);
CGFloat velocityY = (0.2*[sender velocityInView:self.view].y);
CGFloat finalX = translatedPoint.x + velocityX;
CGFloat finalY = translatedPoint.y + velocityY;// translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if (finalX < 0) {
finalX = 0;
} else if (finalX > self.view.frame.size.width) {
finalX = self.view.frame.size.width;
}
if (finalY < 50) { // to avoid status bar
finalY = 50;
} else if (finalY > self.view.frame.size.height) {
finalY = self.view.frame.size.height;
}
CGFloat animationDuration = (ABS(velocityX)*.0002)+.2;
NSLog(@"the duration is: %f", animationDuration);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidFinish)];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
这篇关于如何使用UIPanGestureRecognizer移动对象? iPhone / iPad版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!