iphone-按下按钮时执行操作
例如,我有两个音量按钮(+和-)iphone-按下按钮时执行操作,iphone,objective-c,button,Iphone,Objective C,Button,例如,我有两个音量按钮(+和-) 我如何实现一些功能,例如,当按住+按钮时,它会以一定的间隔递增音量?(我只感兴趣的是在按下按钮时间隔一段时间做一个动作)首先添加按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(increase)forControlEvents:UIControlEventTouchDown]
我如何实现一些功能,例如,当按住+按钮时,它会以一定的间隔递增音量?(我只感兴趣的是在按下按钮时间隔一段时间做一个动作)首先添加按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(increase)forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
然后写下Button press的方法
-(void)increase
{
//increase the volume here
}
如果不是您想要的解决方案,则意味着添加注释…首先添加按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(increase)forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
然后写下Button press的方法
-(void)increase
{
//increase the volume here
}
如果不是您想要的解决方案,则意味着添加注释…假设您有两个按钮,一个用于-另一个用于+ 您可以将间隔信息存储在按钮的标记字段中 在按钮标签属性中安装间隔值。
myPulseButton.tag = 10;
myMinusButton.tag = 10;
[myPulseButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[myMinusButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonEvent:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton == myPulseButton)
{
[self increaseVolume:myPulseButton.tag];
}
else if(myButton == myMinusButton)
{
[self decreaseVolume:myMinusButton.tag];
}
}
使用按钮添加操作。
myPulseButton.tag = 10;
myMinusButton.tag = 10;
[myPulseButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[myMinusButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonEvent:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton == myPulseButton)
{
[self increaseVolume:myPulseButton.tag];
}
else if(myButton == myMinusButton)
{
[self decreaseVolume:myMinusButton.tag];
}
}
执行下面的buttonEvent方法。
myPulseButton.tag = 10;
myMinusButton.tag = 10;
[myPulseButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[myMinusButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonEvent:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton == myPulseButton)
{
[self increaseVolume:myPulseButton.tag];
}
else if(myButton == myMinusButton)
{
[self decreaseVolume:myMinusButton.tag];
}
}
假设您有两个按钮,一个用于-,另一个用于+ 您可以将间隔信息存储在按钮的标记字段中 在按钮标签属性中安装间隔值。
myPulseButton.tag = 10;
myMinusButton.tag = 10;
[myPulseButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[myMinusButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonEvent:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton == myPulseButton)
{
[self increaseVolume:myPulseButton.tag];
}
else if(myButton == myMinusButton)
{
[self decreaseVolume:myMinusButton.tag];
}
}
使用按钮添加操作。
myPulseButton.tag = 10;
myMinusButton.tag = 10;
[myPulseButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[myMinusButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonEvent:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton == myPulseButton)
{
[self increaseVolume:myPulseButton.tag];
}
else if(myButton == myMinusButton)
{
[self decreaseVolume:myMinusButton.tag];
}
}
执行下面的buttonEvent方法。
myPulseButton.tag = 10;
myMinusButton.tag = 10;
[myPulseButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[myMinusButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonEvent:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton == myPulseButton)
{
[self increaseVolume:myPulseButton.tag];
}
else if(myButton == myMinusButton)
{
[self decreaseVolume:myMinusButton.tag];
}
}
您可以使用计时器来完成此操作。触摸启动时启动计时器。如果计时器过期,请增大或减小音量,然后重新启动计时器。触摸结束后,取消计时器。您可以使用计时器进行此操作。触摸启动时启动计时器。如果计时器过期,请增大或减小音量,然后重新启动计时器。触摸结束后,取消计时器