gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:27 24 4
gpt4 key购买 nike

在下面的代码中, 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com