【问题标题】:How to make a button in an animated SubView work during movement of that SubView?如何在子视图移动期间使动画子视图中的按钮工作?
【发布时间】:2014-06-27 01:08:57
【问题描述】:

在下面的代码中,带有按钮的子视图会摆动,即从原点水平移动到红色区域并返回。

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

我想让动画子视图中的这个按钮仅在子视图在 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");
}

【问题讨论】:

  • 我相信你不能在按钮动画时与之交互。尝试使用 NSTimer 并设置按钮框架。
  • 感谢您的回复。现在就试试这个..
  • 只有一个按钮还是多个?
  • @MohitPopat 我在子视图上只有一个按钮,它是动画的。它不会在具有 400 个中心的相同大小的区域之外执行动画...我没有足够的声望点来为你们提供屏幕截图。
  • 我把它给了你,因为你的两个问题。现在享受

标签: ios objective-c uiview uiviewanimation


【解决方案1】:

试试这个希望它会帮助你。取一个数组并将您的按钮添加到该数组中。

@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;
        }
    }
}

【讨论】:

  • 终于有了一个向上的机会,让我能够上传屏幕截图...谢谢伙计,我希望你们现在可以从屏幕截图中得到清晰的想法。
  • 我不知道为什么,但是 for 循环现在可以在这段代码中访问
  • 不,我什至得到了 touchLocation 但仅此而已。
  • 现在有什么问题?
  • 你有没有得到这个日志 NSLog(@"Button Clicked");?
猜你喜欢
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-03
相关资源
最近更新 更多