我在故事板中创建了一个带图像的按钮。突出显示(触摸)时,我希望此按钮更改较大的图像并根据新图像调整大小。故事板已设置,这是我在内部事件中修饰的代码:
if(self.testButton.state == UIControlStateHighlighted)
{
self.testButton.frame = CGRectMake(100, 20, 120, 145);
self.testButton.contentMode = UIViewContentModeScaleAspectFill;
}
NSLog(@"End Frame: ( %f %f %f %f )", self.testButton.frame.origin.x, self.testButton.frame.origin.y, self.testButton.frame.size.width, self.testButton.frame.size.height );
但我的画面尺寸不会改变。我的问题
1.我能用故事板解决这个问题吗? 2.如何用代码解决这个问题?
答案 0 :(得分:-1)
将buttonFrame声明为实例变量并添加目标。
CGRect buttonFrame;
[self.testButton addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
[self.testButton addTarget:self action:@selector(touchUp) forControlEvents:UIControlEventTouchUpInside];
}
- (void)touchDown
{
buttonFrame = self.testButton.frame;
self.testButton.frame = CGRectMake(self.testButton.frame.origin.x, self.testButton.frame.origin.y, 120, 145);
self.testButton.contentMode = UIViewContentModeScaleAspectFill;
}
- (void)touchUp
{
self.testButton.frame = buttonFrame;
}