-3

可能的重复:
使 iPhone 振动

我有两个按钮。一键加,一键减。问题是当我在文本区域中输入某个数字(例如 22)时,手机会振动一段时间。这是我的代码:

我想说的是如果标签显示“22”然后振动电话......问题是我该如何写这个......我还在学习,所以任何有关这方面的帮助将不胜感激!到目前为止,这是我的代码:

#import "StartCountViewController.h"
#import "AudioToolbox/AudioServices.h"

@implementation StartCountViewController


int Count=0;

-(void)awakeFromNib {

    startCount.text = @"0";

}


- (IBAction)addNumber {

    if(Count >= 999) return;

    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", Count++];
    startCount.text = numValue;
    [numValue release];

}

- (IBAction)vibrate {


}
- (IBAction)subtractNumber {

    if(Count <= -35) return;

    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", Count--];
    startCount.text = numValue;
    [numValue release]; 
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
4

1 回答 1

2

这基本上是Programmatically make the iPhone vibrate的副本

话虽如此,我认为您的代码仍然会出现错误,并且语法似乎已被弃用。

这是一个例子。我没有在测试振动所需的实际 iphone 上尝试此操作,但如果您将AudioToolbox 框架添加到项目中,它应该可以工作,当然您的 XIB 文件具有必要的连接:

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *numberLabel;
- (IBAction)addNumber:(id)sender;
- (IBAction)subtractNumber:(id)sender;
@end

视图控制器.m

#import "ViewController.h"
#import "AudioToolbox/AudioServices.h"

@interface ViewController ()
{
  int count;
}
@end

@implementation ViewController
@synthesize numberLabel;

- (void)viewDidLoad
{
  count = 0;
  [self updateCount];
  [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
  [self setNumberLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc 
{
  [numberLabel release];
  [super dealloc];
}

- (IBAction)addNumber:(id)sender 
{
  if(count >= 999) {
    return [self vibrate];
  }; // ignore numbers larger than 999
  count++;
  [self updateCount];
}

- (IBAction)subtractNumber:(id)sender 
{
  if(count <= -35) {
    return [self vibrate];
  }; // ignore numbers less than -35
  count--;
  [self updateCount];
}

-(void)updateCount 
{
  NSString *countStr = [[NSString alloc] initWithFormat:@"%d",count];
  [self.numberLabel setText:countStr];
  [countStr release];
}

-(void)vibrate 
{
  NSLog(@"I'm vibrating");
  AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
@end
于 2012-08-23T03:58:19.030 回答