通过 IF Else 语句以编程方式使 iPhone 振动 [重复]

Posted

技术标签:

【中文标题】通过 IF Else 语句以编程方式使 iPhone 振动 [重复]【英文标题】:make iPhone vibrate programmatically through IF Else statement [duplicate] 【发布时间】:2012-08-22 17:43:54 【问题描述】:

可能重复:Making the iPhone vibrate

我有两个按钮。一键加,一键减。问题是当我在文本区域中输入某个数字(例如 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

【问题讨论】:

让 iPhone 振动:***.com/questions/4724980/making-the-iphone-vibrate 您的vibrate 方法为空。 为什么它是空的,因为我不知道该怎么做。 【参考方案1】:

这基本上是Programmatically make the iPhone vibrate的复制品

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

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

ViewController.h

#import <UIKit/UIKit.h>

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

ViewController.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

【讨论】:

以上是关于通过 IF Else 语句以编程方式使 iPhone 振动 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式使 iPhone 静音 [重复]

如何以编程方式区分 iPhone 和 iPhone(Retina 3.5 英寸)? [复制]

shell脚本编程

良好编程习惯

JAVA编程练习 if-else语句

if-else语句中,if和else的配对原则各是啥