将手指从UIButton移出而不取消选择按钮

时间:2012-02-29 00:46:19

标签: iphone cocoa-touch uiview uikit uibutton

我在我的视图中添加了一个UIButton&我希望每当用户将手指从按钮移开时取消选择该按钮。当我触摸和触摸时,这工作正常将手指向上移动但不在底部。我的代码中是否缺少某些内容:

我的按钮的框架是 - {{7, 8}, {260, 40}}

self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.myButton setTitleColor:[UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1.0] forState:UIControlStateHighlighted];
self.myButton.titleLabel.font = [UIFont boldSystemFontOfSize:kFontSize16];
self.myButton.layer.cornerRadius = 10;
[self.myButton setFrame:CGRectMake(self.bounds.origin.x + 7, self.bounds.origin.y + 8, self.bounds.size.width - 10, 40)];
[self.myButton setBackgroundImage:[[UIImage imageNamed:@"Normal.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0] forState:UIControlStateNormal];
[self.myButton setBackgroundImage:[[UIImage imageNamed:@"Pressed.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0] forState:UIControlStateHighlighted];
[self.myButton addTarget:nil action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.myButton];

另一件事是我正在我的UIView类上实现touchesBegan / touchesEnded方法。

1 个答案:

答案 0 :(得分:0)

您使用的图片尺寸是多少? Normal.png& Pressed.png大小?根据其大小更新您的按钮框架:

假设如果Normal.png是{40, 40}设置您的按钮宽度&身高

self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.myButton setFrame:CGRectMake(7, 8, 40, 40)];

现在运行应用程序并检查。