【问题标题】:How to recognize tapping on the whole oscillating SubView to perform interactive animation如何识别点击整个振荡子视图以执行交互动画
【发布时间】:2014-06-25 10:48:51
【问题描述】:

在下面的代码中,子视图(云)在给定的位置上振荡。 在振荡时点击该子视图,它会移出边界到右侧,然后从左侧移入。 但是点击手势不适用于整个子视图,它仅适用于振荡子视图的右侧。 每当点击云的整个子视图时,我都希望从云中滑出和滑入。

下面分别是.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

【问题讨论】:

    标签: ios iphone objective-c uiview uitapgesturerecognizer


    【解决方案1】:

    我了解您的问题。在 viewwillApper 中,您正在分配 Gesture,在 animateCloud 方法中,您正在分配您的movingCloud1 UIView。

    为了实现这一点,您需要放松一下这些步骤。

    • 在 ViewdidLoad 中,您需要分配 MovingCloud1 视图并将其添加到您的 self.view。

    • 将手势添加到movingCloud1视图后。

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

    【讨论】:

      猜你喜欢
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      相关资源
      最近更新 更多