gpt4 book ai didi

ios - Xcode : Changing Button Colour

转载 作者:行者123 更新时间:2023-11-29 02:51:13 24 4
gpt4 key购买 nike

我现在正在 Xcode 5 中创建一个单一 View 应用程序,但我陷入了困境。有谁知道如何使按钮根据用户的操作改变颜色。例如我有一个按钮,根据用户滑动的方式我想更改按钮的颜色。因此,如果按钮显示向左并且用户向左滑动,按钮将变为绿色,如果用户向右滑动,按钮将变为红色。有人可以帮忙吗?任何帮助将不胜感激。

:)

最佳答案

您可以使用 setBackgroundColor。示例:

[yourButton setBackgroundColor:[UIColor redColor]];

在头部创建方法

-(void)handleSwipeButton : (UISwipeGestureRecognizer*) sw;

将下面的代码添加到viewDidload

UISwipeGestureRecognizer *swLeft = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeButton:)] autorelease];
swLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[yourButton addGestureRecognizer:swLeft];

UISwipeGestureRecognizer *swRight = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeButton:)] autorelease];
swRight.direction = UISwipeGestureRecognizerDirectionRight;
[yourButton addGestureRecognizer:swRight];

并添加到.m文件

-(void)handleSwipeButton : (UISwipeGestureRecognizer*) sw {

if (sw.direction == UISwipeGestureRecognizerDirectionLeft)
[yourButton setBackgroundColor:[UIColor greenColor]];
else
[yourButton setBackgroundColor:[UIColor redColor]];

关于ios - Xcode : Changing Button Colour,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24501230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com