[转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方法将会被调用;
通过UIActionSheetDelegate的actionSheet:didDismisswithButtonIndex方法的buttonIndex来指向
操作的索引;
①如果想通过操作表的destructiveButton按钮来执行某项操作;
if(buttonIndex == [actionSheet destructiveButtonIndex])
{
doSomething;//你要做的操作,显示警报 或是别的动作
}
②通过操作表的Woo按钮来执行某项操作;
if(buttonIndex == [actionSheet firstOtherButtonIndex])
{
doSomething;
}
③通过操作表的cool按钮来执行某项操作;
if(buttonIndex-1 == [actionSheet firstOtherButtonIndex])
{
doSomething;
}
- [转Iphone]iphone开发阶段总结(二)代码知识
- [转Iphone]iphone开发阶段总结(一)
- [转Iphone]iphone开发阶段总结(一)
- [转Iphone]iphone开发阶段总结(一) 基础知识
- [转Iphone]iphone开发阶段总结(一) 基础知识
- iphone开发阶段总结(三)代码示例
- iPhone开发常用代码(二)
- iphone开发零碎知识汇总《二》
- (iPhone/iPad开发)零碎知识总结
- Iphone相关知识二
- iPhone开发学习总结:iPhone应用程序内部构造(二)
- iphone 小代码总结
- iphone 开发笔记二
- iPhone开发笔记二
- iPhone开发常用代码
- iphone开发常用代码
- iPhone开发常用代码
- iphone开发常用代码
- 算法
- 生成[iLow, iHigh]之间的无重复的iCount个int型随机数
- 一个项目涉及到的50个Sql语句(整理版)
- 基于UML的Blog系统分析与设计之四------用例描述
- 热门话题:使用WCF实现SOA面向服务编程—— 架构设计(附原代码)
- [转Iphone]iphone开发阶段总结(二)代码知识
- android 访问权限大全
- 高质量的C/C++编程【某市第九研究所嵌入式软件开发工程师面试题】
- PHP中的define与const
- 一步步学汇编(三)开始编写程序
- CSDN博客首页报错!!
- 行为编辑器
- android之首选项相关 Preferences(四)组织首选项
- 水壶