gpt4 book ai didi

ios - 如何使动画 subview 中的按钮在该 subview 移动期间起作用?

转载 作者:可可西里 更新时间:2023-11-01 04:57:50 24 4
gpt4 key购买 nike

在下面的代码中,带有按钮的 subview 发生振荡,即从原点水平移动到红色区域并返回。

但它不接收按钮本身的点击,而是接收红色区域的点击。

enter image description here

我想让动画 subview 中的这个按钮仅在该 subview 在 x 轴上移动时起作用。

作为这项技术的初学者,我在这里被困住了。

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

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
IBOutlet UIView *viewWithButton;
}
@property (strong, nonatomic) UIView *viewWithButton;
- (void) animateButton;

@end

ViewController.m

@implementation ViewController

@synthesize viewWithButton;

- (void) animateButton
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:10];
[UIView setAnimationRepeatCount:HUGE_VALF];
[UIView setAnimationRepeatAutoreverses:YES];

CGPoint pos = viewWithButton.center;
pos.x = 400;
viewWithButton.center = pos;

[UIView commitAnimations];
}
- (IBAction)btn
{
NSLog(@"Button Tapped");
}

最佳答案

试试这个,希望对你有帮助。取一个数组并将您的按钮添加到该数组。

@property (nonatomic, retain) NSMutableArray *arrBtn;
[self.arrBtn addObject:btn];

把动画做成这样

[UIView animateWithDuration:8.0f
delay:0.0f
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
}
completion:^(BOOL finished){
}];

添加这个方法来取得联系

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIButton *button in self.arrBtn)
{
if ([button.layer.presentationLayer hitTest:touchLocation])
{
// This button was hit whilst moving - do something with it here
NSLog(@"button title is %@",button.titleLabel.text);
[button setBackgroundColor:[UIColor cyanColor]];
NSLog(@"Button Clicked");
break;
}
}
}

关于ios - 如何使动画 subview 中的按钮在该 subview 移动期间起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23562143/

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