如何识别敲击整个振荡子视图来执行交互动画



在下面的代码中,子视图(云)在给定的位置上振荡。在振荡时点击子视图,它会移出边界到右侧,然后从左侧移进来。但是轻拍手势不是在整个子视图上工作,它只在振荡子视图的右端工作。我希望在cloud的整个子视图被点击时,滑动出和滑动入都能工作

下面分别是。h和。m文件的代码。

文件:OscillatingCloudsViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface OscillatingCloudsViewController : UIViewController
{
    IBOutlet UIView *movingCloud1;
    UIImage *cldImg;
}
- (IBAction)animateCloud;
- (IBAction)animateCloudBegin;
@end

文件:OscillatingCloudsViewController.m

#import "OscillatingCloudsViewController.h"
@implementation OscillatingCloudsViewController
- (void) viewWillAppear:(BOOL)animated
{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAnimateCloud)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;
    tapGesture.cancelsTouchesInView = YES;
    [movingCloud1 addGestureRecognizer:tapGesture];
    movingCloud1.userInteractionEnabled = YES;
    [super viewWillAppear:
     animated];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self animateCloud]; 
}
- (IBAction) animateCloud
{
    cldImg = [UIImage imageNamed:@"cloud1.png"];
    movingCloud1=[[UIView alloc]initWithFrame:CGRectMake(50, 50, cldImg.size.width, cldImg.size.height)];
    [movingCloud1 setBackgroundColor:[UIColor colorWithPatternImage:cldImg]];
    [self.view addSubview:movingCloud1];
    movingCloud1.userInteractionEnabled = YES;
    [self viewWillAppear:YES];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:5];
    [UIView setAnimationRepeatCount:HUGE_VALF];
    [UIView setAnimationRepeatAutoreverses:YES];
    CGPoint pos = movingCloud1.center;
    pos.x = 220.0f;
    movingCloud1.center = pos;
    [UIView commitAnimations];
}
- (void) animateCloudHidden
{
    [movingCloud1 setHidden:YES];
}
- (IBAction)animateCloudBegin
{
        movingCloud1.frame = CGRectMake(-100, 50, cldImg.size.width, cldImg.size.height);
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        [UIView setAnimationRepeatCount:1];
        [UIView setAnimationRepeatAutoreverses:NO];
        CGPoint pos = movingCloud1.center;
        pos.x = cldImg.size.width;
        movingCloud1.center = pos;
        [UIView commitAnimations];
}
- (IBAction) tapGestureAnimateCloud
{  
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:2];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationRepeatAutoreverses:NO];
    movingCloud1.center = CGPointMake(1100.0f, 81.5f);
    [UIView commitAnimations];
    [self performSelector:@selector(animateCloudBegin) withObject:nil afterDelay:2.0f];
    [self performSelector:@selector(animateCloudHidden) withObject:nil afterDelay:3.0f];
    [self performSelector:@selector(animateCloud) withObject:nil afterDelay:3.0f];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end

我理解你的问题了。在viewwillApper中,你正在传递手势,并在内部animateCloud方法中分配你的movingCloud1 UIView。

要实现这一点,您需要遵循bello步骤。

  • 在ViewdidLoad你需要分配和添加movingCloud1视图到你的self.view

  • 添加手势到movingCloud1视图后

  • 你的tapGesture现在可以工作了。所以在tapgesture方法中你只需要为movingCloud1视图设置动画

最新更新