【问题标题】:How can I create a reference to an object to animate it in iOS如何创建对对象的引用以在 iOS 中对其进行动画处理
【发布时间】:2012-09-24 04:27:55
【问题描述】:

我有一个imageView,我在里面放了一个按钮。我有两个问题:

  • 如何在不丢失功能的情况下正确添加按钮?
  • 如何在方法中引用我的imageView 来为视图设置动画?

代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *grayFrame = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"expSqrGray.png"]];

    [self.view addSubview:grayFrame];

    grayFrame.frame = CGRectOffset(grayFrame.frame, 0, 0);

    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
    testButton.frame = CGRectMake(20, 20, 200, 44);
    testButton.backgroundColor = [UIColor blackColor];
    [testButton setTitle:@"Test" forState:UIControlStateNormal];
    [testButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];

    [grayFrame addSubview:testButton];
    //[self.view addSubview:testButton];
}

【问题讨论】:

  • 你想做什么样的动画?
  • 只需将带有按钮的图像从底部移动到一定高度即可。
  • 是你想要动画的按钮吗?还是按钮内的图片?
  • 我希望图像和按钮都使用相同的动画。图片将作为背景,我还将在其上添加标签。

标签: ios xcode animation uibutton


【解决方案1】:

我对 Objective-C 知之甚少,但您可能想要做的是为您的类创建 grayFrametestButton 成员变量。这样,您的整个班级都可以访问这两个对象。

例如:

// Test.H
@interface YourClass : NSObject {
   UIImageView * grayFrame;
   UIButton * testButton;
}

然后您可以使用另一种方法来引用其中任何一个。

- (void) initializeObjects {
   grayFrame = [ [ UIImageView alloc ] initWithImage:[ UIImage imageNamed:@"expSqrGray.png" ] ];
   testButton = [ UIButton buttonWithType:UIButtonTypeCustom ];

   // Do things here.
}

【讨论】:

  • 哇,我真傻。我对所有其他事情感到非常困惑,以至于我忘记了这一点。谢谢
猜你喜欢
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多