目标:
代码:
- (void)addMyButton{ // Method for creating button, with background image and other properties
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[self.view addSubview:playButton];
playButton.frame = CGRectMake(150, 150, 100, 100);
//set title,backgroundcolor...
UIImage *buttonImageNormal = [UIImage imageNamed:@"red.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:0 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
if([playButton isTouchInside:[UIImage buttonImageNormal]]) //This line warning
{
UIImage *buttonImageNormal2 = [UIImage imageNamed:@"yellow.png"];
UIImage *strechableButtonImageNormal2 = [buttonImageNormal2 stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal2 forState:UIControlStateNormal];
}
}
请告诉我目前在警戒线上所做的事情。 告诉我有什么错误。 并告诉我'isTouchInside'的语法。 如果你愿意,请实现我的目标。
答案 0 :(得分:1)
尝试更改:
if([playButton isTouchInside:[UIImage buttonImageNormal]])
要:
if([playButton isTouchInside])
isTouchInside不接受任何参数
答案 1 :(得分:1)
您尝试调用名为isTouchInside:
的不存在的方法,该方法将UIImage作为参数。你可能想要isTouchInside
,它不带任何参数。你应该使用那个。
答案 2 :(得分:1)
如果您希望按钮响应触摸事件,也许您应该添加以下代码:
[playButton addTarget: self action:@selector(playButtonPressed:) forControlEvents: UIControlEventTouchUpInside];
然后添加此功能:
- (void) playButtonPressed: (id) sender{
UIImage *buttonImageNormal2 = [UIImage imageNamed:@"yellow.png"];
UIImage *strechableButtonImageNormal2 = [buttonImageNormal2 stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal2 forState:UIControlStateNormal];
}
并删除if([playButton isTouchInside]
)块。这可确保按钮随时响应触摸,而不是仅在创建时响应。