iOS开发之创建并管理图像动画

主要学习UIImageView,UIImage,UISlider,UIStepper的用法,跳动的兔子界面如下:

b_large_XRlK_3be2000015d71261


ViewerControl.M

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{

    NSArray *hopAnimation;
    hopAnimation = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"frame-1.png"], [UIImage imageNamed:@"frame-2.png"],[UIImage imageNamed:@"frame-3.png"],[UIImage imageNamed:@"frame-4.png"],[UIImage imageNamed:@"frame-5.png"],[UIImage imageNamed:@"frame-6.png"],[UIImage imageNamed:@"frame-7.png"],[UIImage imageNamed:@"frame-8.png"],[UIImage imageNamed:@"frame-9.png"],[UIImage imageNamed:@"frame-10.png"],[UIImage imageNamed:@"frame-11.png"],[UIImage imageNamed:@"frame-12.png"],[UIImage imageNamed:@"frame-13.png"],[UIImage imageNamed:@"frame-14.png"],[UIImage imageNamed:@"frame-15.png"],[UIImage imageNamed:@"frame-16.png"],[UIImage imageNamed:@"frame-17.png"],[UIImage imageNamed:@"frame-18.png"],[UIImage imageNamed:@"frame-19.png"],[UIImage imageNamed:@"frame-20.png"], Nil];

    self.bunnyView1.animationImages = hopAnimation;
    self.bunnyView2.animationImages = hopAnimation;
    self.bunnyView3.animationImages = hopAnimation;
    self.bunnyView4.animationImages = hopAnimation;
    self.bunnyView5.animationImages = hopAnimation;
    self.bunnyView1.animationDuration = 1;
    self.bunnyView2.animationDuration = 1;
    self.bunnyView3.animationDuration = 1;
    self.bunnyView4.animationDuration = 1;
    self.bunnyView5.animationDuration = 1;

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setBunnyView1:nil];
    [self setBunnyView2:nil];
    [self setBunnyView3:nil];
    [self setBunnyView4:nil];
    [self setBunnyView5:nil];
    [self setHopsPerSecond:nil];
    [self setSpeedSlider:nil];
    [self setSpeedStepper:nil];
    [self setToggleButton:nil];

    [super viewDidUnload];
}
- (IBAction)toggleAnimation:(id)sender {
    if (self.bunnyView1.isAnimating) {
        [self.bunnyView1 stopAnimating];
        [self.bunnyView2 stopAnimating];
        [self.bunnyView3 stopAnimating];
        [self.bunnyView4 stopAnimating];
        [self.bunnyView5 stopAnimating];
        [self.toggleButton setTitle:@"Hop!" forState:UIControlStateNormal];
    }
    else{
        [self.bunnyView1 startAnimating];
        [self.bunnyView2 startAnimating];
        [self.bunnyView3 startAnimating];
        [self.bunnyView4 startAnimating];
        [self.bunnyView5 startAnimating];
        [self.toggleButton setTitle:@"Sit Still!" forState:UIControlStateNormal];
    }
}

- (IBAction)setSpeed:(id)sender {
    NSString *hopRateString;

    self.bunnyView3.animationDuration = 2 - self.speedSlider.value;
    self.bunnyView1.animationDuration = self.bunnyView3.animationDuration + ((float)(rand()%11+1)/10);
    self.bunnyView2.animationDuration = self.bunnyView3.animationDuration + ((float)(rand()%11+1)/10);
    self.bunnyView4.animationDuration = self.bunnyView3.animationDuration + ((float)(rand()%11+1)/10);
    self.bunnyView5.animationDuration = self.bunnyView3.animationDuration + ((float)(rand()%11+1)/10);

    [self.bunnyView1 startAnimating];
    [self.bunnyView2 startAnimating];
    [self.bunnyView3 startAnimating];
    [self.bunnyView4 startAnimating];
    [self.bunnyView5 startAnimating];

    [self.toggleButton setTitle:@"Sit Still!" forState:UIControlStateNormal];

    hopRateString = [[NSString alloc] initWithFormat:@"%1.2f hps", 1/(2-self.speedSlider.value)];
    self.hopsPerSecond.text = hopRateString;
}

- (IBAction)setIncrement:(id)sender {
    self.speedSlider.value = self.speedStepper.value;
    [self setSpeed:nil];
}
@end
ViewerControll.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *bunnyView1;
@property (strong, nonatomic) IBOutlet UIImageView *bunnyView2;
@property (strong, nonatomic) IBOutlet UIImageView *bunnyView3;
@property (strong, nonatomic) IBOutlet UIImageView *bunnyView4;
@property (strong, nonatomic) IBOutlet UIImageView *bunnyView5;
@property (strong, nonatomic) IBOutlet UILabel *hopsPerSecond;
@property (strong, nonatomic) IBOutlet UISlider *speedSlider;
@property (strong, nonatomic) IBOutlet UIStepper *speedStepper;
@property (strong, nonatomic) IBOutlet UIButton *toggleButton;

- (IBAction)toggleAnimation:(id)sender;
- (IBAction)setSpeed:(id)sender;
- (IBAction)setIncrement:(id)sender;

@end

在Mountain Lion中设置Safari默认的User Agent

设置Safari默认的User Agent为ipad浏览

defaults write com.apple.Safari CustomUserAgent "'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3'"

清除默认项代码

defaults delete com.apple.Safari CustomUserAgent

可以屏蔽些FLASH广告,还可以用HTML5来看视频去广告~

retain和copy的区别

assign就不用说了,因为基本上是为简单数据类型准备的,而不是NS对象们。

#Retain vs. Copy

copy : 建立一个索引计数为1的对象,然后释放旧对象
retain :释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1

那上面的是什么该死的意思呢?
Copy其实是建立了一个相同的对象,而retain不是
比如一个NSString 对象,地址为0×1111,内容为@”STR”Copy 到另外一个NSString 之后,地址为0×2222 ,内容相同,新的对象retain为1,旧有对象没有变化retain 到另外一个NSString 之后,地址相同(建立一个指针,指针拷贝),内容当然相同,这个对象的retain值+1也就是说,retain 是指针拷贝,copy 是内容拷贝。
哇,比想象的简单多了… 自己研究了一下,,上面文章的说法是对的,,但是遗漏了一点东西.他说 copy是内容的拷贝 ,对于像NSString,的确是这样.但是,如果是copy的是一个NSArray呢?

比如,

NSArray *array = [NSArray arrayWithObjects:@"hello",@"world",@"baby"];
NSArray *array2 = [array copy];

这个时候,,系统的确是为array2开辟了一块内存空间,但是我们要认识到的是,array2中的每个元素,,只是copy了指向array中相对应元素的指针.这便是所谓的”浅复制”.了解到这一点非常重要….常在声明一些成员变量时会看到如下声明方式:@property (参数1,参数2) 类型 名字;这里我们主要分析在括号中放入的参数,主要有以下三种:setter/getter方法(assign/retain/copy)读写属性(readwrite/readonly)atomicity(nonatomic)其中各参数说明如下:assign默认类型,setter方法直接赋值,而不进行retain操作retainsetter方法对参数进行release旧值,再retain新值,如下代码:

-(void) setObj:(ClassX*) value
{
if (obj != value)
    {
        [obj release];
        obj = [value retain];
    }
}

setter方法进行Copy操作,与retain一样nonatomic禁止多线程,变量保护,提高性能readwrite产生setter\getter方法readonly只产生简单的getter,没有setter。

Opening My Sites

Today is a lucky day, I finally have my first sites on Internet.

We often lose many beautiful moments in our lives. So I decided to record my moments on the net, whether you or me, witnessing my growth together.