ios - 如何识别点击整个振荡 subview 以执行交互式动画

标签 ios iphone objective-c uiview uitapgesturerecognizer

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

下面分别是.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 中,您正在评估 Gesture,并在 animateCloud 方法中分配您的 movingCloud1 UIView。

要实现这一点,您需要执行 bello 步骤。

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

  • 将 Gesture 添加到 movingCloud1 View 后。

  • 您的 tapGesture 现在可以使用了。因此,在您的 tapsture 方法中,您只需要为 movingCloud1 View 设置动画

关于ios - 如何识别点击整个振荡 subview 以执行交互式动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23535083/

相关文章:

ios - 以编程方式添加约束时应用程序崩溃

iphone - 为什么在iOS上升级时需要卸载Trigger.IO app?

iphone - 长按自定义注释图钉更改为默认红色图钉

iOS/iPad 应用程序,在本地保存数据

objective-c - 使用dictionaryWithObjects时重复键会发生什么情况:forKeys:?

ios - 将实例化的 UIView 从 NIB 约束到其他 UIView

ios - 对于具有 lineSpacing 和多种颜色的单行文本,UILabel 大小不正确

android - 为什么在 flutter 中的 build 者在这里破裂

ios - 从任何 YouTube 视频获取流媒体 URL

ios - 如何桥接 Objective-C initWithError : method into Swift