[转Iphone]iphone开发阶段总结(二)代码知识

来源:互联网 发布:aide源码 编辑:程序博客网 时间:2024/06/06 03:46

1.实现图像的移动

NSTimer *timerArrow;

 

-(IBAction)shoot

{

     timerArrow = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:@selector(onTimerArrow) userInfo:nil repeats:YES];

}

 

-(void) onTimerArrow {

NSLog(@"go go go");

//transform 后的参数为图像移动的x,y的距离

arrowView.transform = CGAffineTransformTranslate(arrowView.transform, 0, -3.0);

}

 

2. 实现图像的旋转可以有两种实现方式(UIImageView *imageView)

①. 使用CGAffineTransformMakeRotation类的类方法CGAffineTransformRotate方法。

imageView.transform = CGAffineTransformRotate(imageView.transform, -0.02);

 

②. 使用CGAffineTransformMakeRotation类的类方法CGAffineTransformMakeRotate方法。

float angle = 0;

angle += -0.02;

 

imageView.transform = CGAffineTransformMakeRotation(angle);

 

3.时间条的读取(UIProgressView *pg)

int progress = 0;

progress++;

//时间条读取的速度,取决于时间的多少(此处为30秒的倒数[1/30])

[pg setProgress:progress*0.033];

 

4.屏幕轻击、触摸事件

①.点击次数

//NSUInteger为无符号的整形

NSUInteger numTaps = [[touches anyObject] tapCount];

NSString *tapsMessage = [[NSString alloc] initWithFormat:@"%d taps detected ", numTaps];

 

②.触摸开始

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

}

 

③.触摸移动

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

}

 

④.触摸取消,通常为内存不够用的时候调用

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

}

⑤.触摸结束

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

}

 

⑥.轻扫屏幕

.h

UILabel *label;

CGPoint marker;

 

.m

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];

marker = [touch locationInView:self.view];

}

 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];

CGPoint moved = [touch locationInView:self.view];

 

CGFloat deltaX = fabsf(marker.x - moved.x);

CGFloat deltaY = fabsf(marker.y - moved.y);

 

if (deltaX >= 25 && deltaY <= 5)

{

label.text=@" yes yes yes";

}

else if(deltaX <= 5 && deltaY >=25)

{

label.text=@" no no no no";

}

}

 

 

5. 循环执行一个方法可以有两种实现方法

①. 使用NSTimer类的相关方法。

NSTimer *timer;

int count=0;

-(void)do

{

     NSLog(@"Test timer");

     count++;

     //10秒之后timer停止

     if(count >= 10)

     {

         [timer invalidate];

     }

}

 

- (void)viewDidLoad {

    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(do) userInfo:nil repeats:YES];

}

 

②. 使用NSObject类的performSelector:withObject:afterDelay方法。

-(void)do

{

     NSLog(@"Test timer");

     [self performSelector:@selector(do) withObject:nil afterDelay:1];

}

 

- (void)viewDidLoad {

    [self do];

}

 

6.根据选择的按钮,将按钮的title传给标签并显示出来(UILable *status)

   NSString *title = [sender titleForState:UIControlStateNormal];

   NSString *newText = [[NSString alloc] initWithFormate:#" you press the %@" ,title];

   status.text= newText;

   [newText release];

 

7.点击done关闭键盘 (UITextField *nameField)

-(IBAction)nameFieldDoneEditing:(id)sender;

-(IBAction)nameFieldDoneEditing:(id)sender

{

     [sender resignFirstResponder];

}

 

   点击屏幕关闭键盘

-(IBAction)backgroundClick:(id)sender;

-(IBAction)backgroundClick:(id)sender

{

    [nameField resignFirstResponder];

}

8.通过UISegmentedControl 控制UIView(UIView *switch) 显示或是隐藏的状态;

- (IBAction)toggleShowHide:(id)sender

{

UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;

//查询uisegmentcontrol的值

NSInteger segment = segmentedControl.selectedSegmentIndex;

 

if (segment == 0)

[switchView setHidden:NO];

else

[switchView setHidden:YES];

 

}

 

9.UISwitch (UISwitch *soundSwitch) 开关的控制

-(IBAction)switchChanged:(id)sender

{

    UISwitch *whichSwitch = (UISwitch *)sender;

    BOOL setting = whichSwitch.isOn;

    [SoundSwitch setOn:setting animated:YES];

}

 

10.操作表

使用操作表必须符合<UIActionSheetDelegate>协议(.h头文件里的UIViewController后添加)

同时操作表是模式化的(模式化就是程序显示操作表时,程序的其他任何部分都是不可操作的,

必须在操作表上作出选择,才可以执行其他的操作)

-(IBAction)doSomething:(id)sender

{

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"ready?"

delegate:self

cancelButtonTitle:"No"

destructiveButtonTitle:"Yes"

otherButtonTitle:@"Woo",@"Cool",nil];

    [actionSheet showInview:self.view];

    [actionSheet release];

}

里面的delegate:self 通过将self做为委托参数传递给actionsheet:didDisMissWithButtonIndex:

actionSheet的方法,当按钮被按下的时候,委托的actionsheet:didDisMissWithButtonIndex:

actionSheet方法将会被调用;

 

通过UIActionSheetDelegateactionSheet:didDismisswithButtonIndex方法的buttonIndex来指向

操作的索引;

如果想通过操作表的destructiveButton按钮来执行某项操作;

if(buttonIndex == [actionSheet destructiveButtonIndex])

{

    doSomething;//你要做的操作,显示警报 或是别的动作

}

通过操作表的Woo按钮来执行某项操作;

if(buttonIndex == [actionSheet firstOtherButtonIndex])

{

    doSomething;

}

通过操作表的cool按钮来执行某项操作;

if(buttonIndex-1 == [actionSheet firstOtherButtonIndex])

{

    doSomething;

}

原创粉丝点击