您的位置:首页 > 移动开发 > IOS开发

iOS笔记 (3)

2015-08-22 09:59 411 查看
十.忙碌指示器

    UIActivityIndicator。
1. UIActivityIndicatorView *activityIndicatior = [UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge]; 
2.     activityIndicator.center = CGPointMake(512, 384); 
3.     [self.view addSubview: activityIndicator]; 
4.     [activityIndicator startAnimating];  
5.     [activityIndicator stopAnimating]; 
6.     [activityIndicator removeFromSuperView]; 
十一.媒体播放

1- 音频

    [1] 音乐往往是存储在iPod曲库(注意位置)中的,可以通过媒体选择器(media picker)或者媒体查询(media query)读取,然后用音乐播放器MPMusicPlayerController播放。
1. MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer]; 
2. [musicPlayer setShufleMode: MPMusicShuffleModeSongs]; 
3. [musicPlayer setRepeatMode: MPMusicRepeatModeAll]; 
4. [musicPlayer setQueueWithQuery: [MPMediaQuery songsQuery]; 
5. [musicPlayer play]; 
   applicationMusicPlayer返回的播放器,在你的应用中播放音乐。它不会影响到iPod播放器,也不能从iPod播放器重获取信息。

    iPodMusicPlayer返回的是iPod播放器,在你推出应用后,所有的设置都会影响到之后设备上的iPod播放器。

    获得音乐播放器后,需要为它设置一个播放队列。可以用setQueueWithQuery:放方法,通过媒体查询MPMediaQuery来设置 播放队列,也可以用setQueueWithItemCollection:方法,通过MPMdiaItemCollection来设置播放队列。

    重复模式repeatMode可以设置为不重复、重复当前曲目、或整个播放列表;乱序播放shuffleMode可以设置为不乱序、乱序播放曲目或乱序播放专辑;音量volume的设置与音频播放器一样。

    skipToNextItem跳到下一首,skipToPreviousItem跳到上一首,skipToBegin跳到第一首。

    对应的宏都是以MPMusic开头。

    [2] 利用系统声音服务来播放短暂音效(时长30秒以内),并震动:   
1. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  
    播放指定音效:
1. NSURL *fileURL = [NSURL fileURLWithPath: path isDirectory: NO];  
2. // 创建音效ID 
3. SystemSoundID soundID; 
4. AudioServiceCreateSystemSoundID((CFURLRef) fileURL, &soundID); 
5. // 播放声音 
6. AudioServicesPlaySystemSound(soundID); 
   [3] 音频播放器

    没有时长限制
1. NSURL *fileURL = [NSURL fileURLWithPath: path isDirectory: NO]; 
2. // 用URL来初始化音频播放器-播放的不是iPod曲库中的音乐 
3. AVAudioPlayer* player = [AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: NO]; 
4. // 准备播放 
5. [player prepareToPlay]; 
6. // 设置代理 
7. [player setDelegate: self]; 
   方法:play、pause、stop。可以通过playing属性查询播放器是否正在播放当中,可以通过volume属性来修改和查询播放器的播放增益(从0.0到1.0),可通过setting属性查询播放器其他设置。

    duration表示音频的时间长度, currentTime表示当前播放到的时间。播放结束后可以通过代理方法audioPlayerDidFinishPlaying:来处理播放后设置。

2- 视频

    视频播放可以采用网页视图(UIWebView)进行嵌入式播放(能播放YouTube视频),或者采用电影播放器(MPMoviePlayerController)进行播放。

    [1] 电影播放器
1. MPMoviePlayerController *player = [MPMoviePlayerController alloc]initWithContentURL: url]; 
2.     // 设置播放器的大小,并将其加入视图中 
3.     [player.view setFrame: rectFrame]; 
4.     [self.view addSubView: player.view]; 
5.     播放器的背景视图backgroundView。 
6.     全屏[player setFullscreen: YES animated: YES]; 
7.     播放另一个影片[player setContentURL: newURL]; 
8.     [player requestThumbnailImagesAtTimes:arrayTimes timeOption:MPMovieTimeOptionNearestKeyFrame]; // 表示播放器不会在你所指定的时间去截取预览,而是在绝对时间的附近几帧中寻找效果最好的帧做为预览。 
scalingMode规定了影片的缩放模式。   
initialPlaybackTime用来控制视频开始播放的时间,单位是秒。
如果视频源在网络上,那么需要正确设置服务器端的mimeType。
十二.运行环-runloop

    运行环作用于一个iOS应用的整个生命周期。它负责监视各种输入事件,并且在合适的时候对这些输入进行分配。应用的每一个线程都有且仅有一个运行环。你自己不需要创建也不需要销毁运行环,但是可以通过currentRunLoop方法来获取当前的运行环。

十三.定时器

    由于运行环机制,定时器的精度不高,只能用于一般性延时。

    例子:拼图游戏,DeskViewController.m。

    NSObject类的定时方法。

    performSelector: withObject: afterDelay: 运行方法,参数,时间(秒)。

    performSelectorOnMainThread: withObject: waitUntilDone:在主线程中,运行参数selector所指定的方法,如果waitUntilDone参数为YES,那么当前线程会被阻拦,直到selector运行完。

    performSelector: onThread: withObject: waitUntilDone:同上,但不一定在主线程中运行。

    performSelectorInBackground: withObject: 开启一个新线程,用于运行selector方法,selector方法应负责线程的初始化。

    cancelPreviousPerformRequestsWithTarget:取消与一个目标相关的所有计划好的动作。

    cancelPreviousPerformRequestsWithTraget: selector:object只取消特定的计划动作。
十四.随机数

0~1之间随机数
1. CG_INLINE float genRandomNum() 
2. { 
3.     return (float)arc4random/ARCRANDOM_MAX; 
4. } 
十五.加速度传感器

    获得加速度传感器读数:
1. UIAccelerometer *accelerometer = [UIAccelerometer shareAccelerometer]; // 单例 
2. accelerometer.delegate = self; 
3. accelerometer.updateInterval = 1.0f / 10.0f; // 传感器更新频率,普通游戏10~30HZ,虚拟现实类游戏,30~60HZ 
4. accelerometer.x, accelerometer.y, accelerometer.z // 传感器读数 
   若不想再接受传感器读数,可将代理设置为nil
1. [UIAccelerometer shareAccelerometer].delegate = nil;  
2.  
3. #define alpha 0.05 
4. // 获得重力引起的加速度读数: 
5. - (void)accelerometer: (UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceleration 
6. { 
7.     // 简单低通滤波器 
8.     accelX = (acceleration.x * alpha) + (accelX) * (1.0 - alpha); 
9.     accelY = (acceleration.y * alpha) + (accelY) * (1.0 - alpha); 
10.    accelZ = (acceleration.z * alpha) + (accelZ) * (1.0 - alpha); 
11.} 
12.     
13.// 获得动作引起的加速度读数: 
14.- (void)accelerometer: (UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceleration 
15.{ 
16.    // 减去低通滤波输出,得到高通滤波输出 
17.    accelX = acceleration.x - ((acceleration.x * alpha) + (accelX) * (1.0 - alpha)); 
18.    accelY = acceleration.y - ((acceleration.y * alpha) + (accelY) * (1.0 - alpha)); 
19.    accelZ = acceleration.z - ((acceleration.z * alpha) + (accelZ) * (1.0 - alpha)); 
20.} 
十六.地图与连接服务器
[1] 添加框架MapKit.framework。使用MKMapView来呈现地图。注意应当直接使用此类,而不是继承之。如果希望在MKMapView类之上添加功能,可以使用MKMapViewDelegate协议。

    初始化:
1. MKMapView *mapView = [MKMapView alloc] initWithFrame: rect]; 
   初始化之后并不直接显示,还需要指定显示的地图区域:
1. CLLocationCoordinate2D coordinate; 
2. coordinate.latitude = latitudeValue; // 纬度 
3. coordinate.longtitude = longtitudeValue; // 精度 
4. mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, width, height); // 指定显示区域,width和height单位都是米 
   之后可以通过addSubview添加地图。

    属性:showsUserLocation-为YES,系统会持续跟踪用户的位置

          userLocationVisible-为YES,将显示用户所在位置

    显示地图之后,常常希望在地图上添加标注,这需要创建一个类,并实现MKAnnotation协议,这个类叫做标注对象。标注对象往往实现setCoordinate:方法来设置其坐标。在地图视图上,可以设置标注对象的坐标,然后添加进去,这样地图上就会出现一个标注。代理方法title 和subtitle能够在标注上显示标题和副标题。  
1. // 初始化 
2.    mapView = [MKMapView alloc] initWithFrame: CGRectMake(100, 100, 550, 700)]; 
3.    mapView.showsUserLocation = TRUE; 
4.    mapView.mapType = MKMapTypeStandard; 
5.    mapView.delegate = self; 
6.    // 设置坐标 
7.    CLLocationCoordinate2D coordinate; 
8.    coordinate.latitude = 37.31; 
9.    coordinate.longtitude = -122.03; 
10.   mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 4000, 6000); // 4000米宽,6000米高的区域 
11.   [self.view insertSubview: mapView atIndex: 0]; 
十七.通过storyboard来初始化一个controller
1. CBigDesignImageViewController *imageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"BigImageController"];  
  BigImageController是要在storyboard中设置的Identifier属性。

十八.给UIView子类设置阴影,包括各种控件

    1 导入QuartzCore framework

    2 #import <QuartzCore/QuartzCore.h>

    3 编码:
1. [imgView layer] setShadowOffset:CGSizeMake(5, 5)]; // 阴影的范围  
2. [imgView layer] setShadowRadius:2]; // 阴影扩散的范围控制  
3. [imgView layer] setShadowOpacity:1]; // 阴影透明度  
4. [imgView layer] setShadowColor:[UIColor brownColor].CGColor]; // 阴影的颜色 
十九.设置UIScrollView滚动速度      
1. // 自动滚动太快,效果不好,这里把动画设置慢点,注意下面要直接赋值contentOffset,不要用带animated参数的函数,否则动画会出问题,因为两处都是动画效果。 
2.         [UIScrollView animateWithDuration:1.0f  
3.                                delay:0  
4.                                options:UIViewAnimationCurveLinear 
5.                                animations:^{ 
6.                                    scrollView.contentOffset = CGPointMake(0, 0);  
7.                                } 
8.                                completion:^(BOOL finished){} 
9.                                ];  
10.        // 如果在减速滚动过程中,按了刷新按钮,执行上面的动画,会出现重置的位置,y不是0的情况,这里再调用一次,滚动到0。 
11.        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];  
二十.EXC_BAD_ACCESS

Here’s the advice I generally give todevelopers when you hit an EXC_BAD_ACCESS error:

-Set the NSZombieEnabled argument in your executable options, which sometimeshelps narrow down the cause

-Run with Apple Instruments such as Leaks to look for memory issues

-Set a breakpoint in your code and step through until you narrow down where it’s crashing

Tried and true “comment out code till it works” then backtrack from there :]

Xcode4 下设置 NSZombieEnabled 的方法: 

你可以点击 Xcode4 菜单 Product ->Edit Scheme -> Arguments, 然后将点击”加号”, 将 NSZombieEnabled 参数加到 Environment Variables 窗口中, 后面的数值写上 ”YES”.

或者在 Xcode4 菜单 Product ->Edit Scheme -> Diagnostics 设置窗口中直接勾上 Enable ZombieObjects 即可,Xcode 可用 cmd+shift+< 进到这个窗口。 

Xcode4 已经考虑到了现在的要求,所以提供了更便捷的设置的方式,你也可以在这个窗口中设置其他一些参数,你肯定能由此获得更多的帮助信息。
 
好了,看完这篇iOS学习笔记整理,不知道对你是不是有所启发
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: