我有一个简单的问题,但我不知道为什么它不起作用。 我以编程方式创建了一个按钮,我想将方法“backLogin”添加到该操作。 奇怪的是,我没有得到任何错误,但按钮不会以任何方式回应。我在互联网上看到的任何地方,这段代码都应该可以工作。 任何想法我做错了什么?自行创建的按钮后面的操作不起作用。
-(void) loginAppear{
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 75, 37); // position in the parent view and set the size of the button
[myButton setTitle:@"Back" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[myButton setAlpha:1];
[loginImage addSubview:myButton];
}
后面 “backLogin” 的代码已经先后在其他情况:不是添加到为myButton的loginImage
-(void) backLogin{
CALayer *layer = loginImage.layer;
//spin frame
CABasicAnimation *anim1 =
[CABasicAnimation animationWithKeyPath: @"transform" ];
anim1.toValue = [ NSValue valueWithCATransform3D:
CATransform3DMakeRotation(180 * M_PI, 0.0, 1.0, 0) ];
anim1.duration = .2;
anim1.cumulative = YES;
anim1.repeatCount = 1;
[layer addAnimation: anim1 forKey: @"transformAnimation" ];
}
你试过在你的backLogin方法中放一个NSLog,来确认是否调用该方法......也许这里的代码不工作 – Quirin
什么是loginImage?你有没有看到按钮被按下的视觉指示? –
你好,loginImage是一个UIImageView实例。我现在会尝试NSLog .. – Trace