始点と移動点からエンドポイントを見つける必要があります。
私はアニメーションを行っていView
ますが、ユーザーがビューをドラッグするときに移動する必要があり、それを画面の外に出して元のポイントに戻す必要があります。
現在、Move でのスワイプを検出するためにUISwipeGestureRecognizerを使用しています。以下はコードです。
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
// Adding the swipe gesture on image view
[_view1 addGestureRecognizer:swipeLeft];
[_view1 addGestureRecognizer:swipeRight];
[_view1 addGestureRecognizer:swipeUp];
[_view1 addGestureRecognizer:swipeDown];
スワイプの処理
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
CGPoint movedPoint = [swipe locationInView:swipe.view];
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Left Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find END POINT BASED ON THIS DATA
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Right Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"Up Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"Down Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
}
No.1ビューをスワイプすると、SwipeHandler(handleSwipe)メソッドで移動ポイントを取得できます
そのため、スワイプの方向も検出できます。しかし、私の問題は、No.1 View を画面外に出さなければならないことです。そのためには、エンドポイントを見つける必要があります。
では、開始点と MovedPoint から Endpoint を見つけるにはどうすればよいですか?