gpt4 book ai didi

ios - animateWithDuration 和 touchesBegan 不能成对工作

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

当我的对象在我的屏幕上移动时,我需要处理触摸。当调用 touchesBegan 时,我需要隐藏我的对象。

这是我在 UIViewController 中的代码:

- (void)win {
for (NSInteger i = 1; i <= 6; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"rocket_%d.png", i]]];
imageView.tag = 1000;
CGSize size = imageView.image.size;
[imageView setFrame:CGRectMake(200 + (i * 60), 500, size.width, size.height)];
[self.view addSubview:imageView];
[rockets addObject:imageView];
[imageView setUserInteractionEnabled:YES];
}
[self startRockets];
}

- (void)startRockets {

CGFloat timeInterval = 1.0;
for (UIImageView *imageView in rockets) {
[UIView animateWithDuration:5.0 delay:timeInterval options:UIViewAnimationOptionAllowUserInteraction animations:^{
[imageView setFrame:CGRectMake(imageView.frame.origin.x, 0, imageView.frame.size.width, imageView.frame.size.height)];
} completion:nil];
timeInterval += 1.0;
}


}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSArray *views = [self.view subviews];
for (UIView *v in views) {
if (v.tag == 1000) {
if (CGRectContainsPoint(v.frame, touchLocation) == YES) {
[v setHidden:YES];
}
}
}
}

最佳答案

也许您缺少 AllowAnimatedContent。尝试改变你的

UIViewAnimationOptionAllowUserInteraction

UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent


...另一种方法可能是使用 UITapGestureRecognizer :

- (void)createViews {
rockets = [[NSMutableArray alloc] init];
for (NSInteger i = 1; i <= 6; i++) {
UIView *view = [UIView new];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake((i * 60), 100, 50, 50);

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rocketTouched:)];
[view addGestureRecognizer:tap];

[self.view addSubview:view];
[rockets addObject:view];
}
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(moveViews) userInfo:nil repeats:YES];
}
- (void)moveViews {
for (UIView * view in rockets) {
[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionAllowAnimatedContent animations:^{
view.frame = CGRectMake(view.frame.origin.x -10, 0, view.frame.size.width, view.frame.size.height);
} completion:nil];
}
}
- (void)rocketTouched:(UITapGestureRecognizer*)tap {
tap.view.hidden = YES;
}

关于ios - animateWithDuration 和 touchesBegan 不能成对工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13431806/

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