6

画面上を移動するアニメーションを作成しました。アニメーションは継続的にループします。アニメーション画像をタップするとアニメーションを停止し、タッチを離すとアニメーションを続行するにはどうすればよいですか?

TouchesMoved を使用して、指定したボタンを次のように移動する方法を知っています。

CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;

しかし、それを私のアニメーションで動作させる。触った後もアニメーションを続けてほしい。

SelectedCellViewController.h

//  SelectedCellViewController.h

#import <Accounts/Accounts.h>
#import <QuartzCore/QuartzCore.h>

@interface SelectedCellViewController : UIViewController {
IBOutlet UIImageView *imageView;
UIImageView *rocket;
}

@end

viewControllertoShow.m

#import "SelectedCellViewController.h"

@interface SelectedCellViewController ()

@end

@implementation SelectedCellViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad {

[super viewDidLoad];

[self performSelector:@selector(imageSpawn:) withObject:nil afterDelay:3];

}

- (void) imageSpawn:(id) sender
{

UIImage* image = [UIImage imageNamed:@"ae"];
rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 200, 25, 40);
[UIView animateWithDuration:5
                      delay:0.2f
                    options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                 animations:^(){rocket.frame=CGRectMake(345, 200, 25, 40);}
                 completion:^(BOOL fin) {
                 }];

[self.view addSubview:rocket];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[rocket addGestureRecognizer:tapped];
[rocket setUserInteractionEnabled:YES];


}

-(void)ballTapped:(UIGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:gesture.view];

//then write code to remove the animation
[self.view.layer removeAllAnimations];
NSLog(@"Tag = %d", gesture.view.tag);
rocket.frame = CGRectMake(location.x,location.y,25,40);
}

- (void)dismissView {
[self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)viewDidUnload {

}

@end
4

3 に答える 3

3

質問ですでに述べたように、使用してタッチポイントを取得できます

CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];

次に、この点がアニメーションの座標内にあるかどうかを確認してから、アニメーションUIImageviewを停止します。UIViewただし、scrollview を使用している場合、scrollview はタッチ イベントを返さないため、これを使用することはできません。

画像ビューがアニメーション化されているため、サブビューを追加するときに、次のように UITapGestureRecogniser を画像ビューに追加することをお勧めします。

- (void) imageSpawn:(id) sender
{

UIImage* image = [UIImage imageNamed:@"ae"];
UIImageView *rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 200, 25, 40);
[UIView animateWithDuration:5
                      delay:0.2f
                    options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                 animations:^(){rocket.frame=CGRectMake(345, 200, 25, 40);}
                 completion:^(BOOL fin) {
                 }];

[myScrollView addSubview:rocket];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ballTapped:)];
    tapped.numberOfTapsRequired = 1;
    [self.rocket addGestureRecognizer:tapped];
    [rocket setUserInteractionEnabled:YES];
}

ターゲット関数で、アニメーションを停止するコードを記述します。

    -(void)ballTapped:(UIGestureRecognizer *)gesture
    {
    //here also you can get the tapped point if you need
        CGPoint location = [gesture locationInView:gesture.view];

    //then write code to remove the animation
        [self.view.layer removeAllAnimations]; 
     }

編集:

タッチしたポイントで画像ビューを停止しようとしている場合は、これを ballTapped イベントに追加できます。

rocket.frame = CGRectMake(location.x,location.y,25,40);

ただし、このためには、UIImageView *rocketこの特定のメソッドの外で宣言する必要があります。つまり、ヘッダー ファイルで宣言します。

于 2013-03-28T12:50:04.383 に答える
1

別の方法は、これをアニメーションの親ビューに追加することです -

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    self.animating = NO;
    return [super hitTest:point withEvent:event];
}

(アトミック) プロパティを使用し、アニメーションでプロパティをチェックして、停止する必要があるかどうかを確認します。これを使用して、実行中のフォト ギャラリーを停止し、ユーザーが手動で写真を移動できるようにします。必要に応じて、ポイントが特定のエリアにあるかどうかをここでチェックインすることもできます。このメソッドは、タッチ メソッドが呼び出される前に実行されます。

于 2013-03-28T13:19:41.633 に答える
0

このようにしてみてください。アニメーションの途中でイメージビューに触れると、アニメーションがビューから削除されます。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint lastLocation = [touch locationInView: self.view];

    if([[touch view] isKindOfClass:[UIImageView class]]){
        [youImageView.layer removeAllAnimations]; 
        youImageView.center=lastLocation;//assign your image view center when the animation removes from the view.
    }
}

フレームワークを追加し #import <QuartzCore/QuartzCore.h>ます。

于 2013-03-28T13:03:56.847 に答える