将手指从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方法。

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

假设Normal.png是{40, 40},则设置按钮宽度&高度为

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

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

最新更新