IOS-0812-圖片瀏覽&Tom 貓(筆記)

1.收尾式動畫

nil 是啥

1>準備開始一個動畫
    [UIView beginAnimations:nil context:nil];
    設置動畫執行的時長
    [UIView setAnimationDuration:1.0];
2>修改控件屬性的代碼,就可以實現動畫。
    self.imageButton.frame=frame;
3>提交動畫
    [UIView commitAnimations];

加了動畫效果後圖片運動就會變得平滑。

接下來介紹動畫
@property(nonatomic) CGAffineTransform transform;
控件的形變屬性(可以設置旋轉角度,比例縮放,平移等屬性)

c語言的三元運算符”:
CGFloat dx = 0, dy = 0;
if (button.tag == kMovingDirTop || button.tag == kMovingDirBottom) {
dy = (button.tag == kMovingDirTop) ? -kMovingDelta : kMovingDelta;
}
if (button.tag == kMovingDirLeft || button.tag == kMovingDirRight) {
dx = (button.tag == kMovingDirLeft) ? -kMovingDelta : kMovingDelta;
}

// CGAffineTransformMakeTranslation的位移形變是相對按鈕"初始"位置來變化的
    self.iconButton.transform = CGAffineTransformMakeTranslation(0, self.delta);
    // CGAffineTransformTranslate 的位移形變是對按鈕的上次形變的累加
     self.iconButton.transform = CGAffineTransformTranslate(self.iconButton.transform, dx, dy);

after updating to ios8, the code may like this to works fine, but in ios7 did not work:
cgaffinetransformmaketranslation-and-cgaffinetransformscale-transforms-different

同理

/*放大縮小*/
-(IBAction)zooms:(UIButton*)button{
    CGFloat scale=(button.tag)?1.2:0.8;
    self.imageButton.transform=CGAffineTransformScale(self.imageButton.transform,scale,scale);
}

看看系統源碼

struct CGAffineTransform {
  CGFloat a, b, c, d;
  CGFloat tx, ty;
};

官方文檔

這裏寫圖片描述

如果 transform 加動畫會這樣子
如果frame旋轉加動畫 會圖片直接沒有
frame不建議直接修改

總結:

frame 屬性,通常用於實例化控件,指定初始位置
如果需要改變控件位置,可以使用center屬性
如果需要改變控件大小,可以使用bounds屬性

  • center加旋轉就是我們預想的上下左右移動

  • 旋轉會改變圖片大小(因爲圖片要被正方形框起來)

  • ios的佈局 後加的元素層疊在上面 (Android 一樣)

/*加載完成被調用*/
- (void)viewDidLoad {
    //千萬不要忘記調用父類的實現方法
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    btn.backgroundColor=[UIColor redColor];
    //設置背景圖片
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted];

    //設置文字
    [btn setTitle:@"點我啊" forState:UIControlStateNormal];
    [btn setTitle:@"摸我" forState:UIControlStateHighlighted];

    //設置按鈕文字顏色
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];

    //文件垂直對齊方式
    btn.contentVerticalAlignment=UIControlContentVerticalAlignmentBottom;

    //將按鈕添加到視圖
     [self.view addSubview:btn];


    //這樣添加的就是custom的模式
    //使用alloc init方法實例化的按鈕,就是custom類型的,按鈕的類型一旦指定,不能修改
    //如果創建其他類型的按鈕
    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeContactAdd];//顯示一個藍色加號
    btn1.center=CGPointMake(20, 40);
    [self.view addSubview:btn1];
    //當然還有很多其他的類型的button   

源代碼 系統還有的圖片類型

    typedef NS_ENUM(NSInteger, UIButtonType) {
        UIButtonTypeCustom = 0,                         // no button type
        UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button

        UIButtonTypeDetailDisclosure,
        UIButtonTypeInfoLight,
        UIButtonTypeInfoDark,
        UIButtonTypeContactAdd,
       // 圓角矩形
        UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead
    };
 }

iPhone的UI控件UIImage和UIImageView有什麼區別呢?

uiimage 是圖片,不是控件;他的父類爲NSObject;
UIImageView是加載圖片的控件,父類爲UIView

案例一:圖片查看器

//
//  ViewController.m
//  0812-圖片查看器
//
//  Created by susan on 16/1/12.
//  Copyright © 2016年 susan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) UILabel *lable;
@property (nonatomic,strong) UIImageView *Image;
@property (nonatomic,strong) UIButton *LeftButton;
@property (nonatomic,strong) UIButton *RightButton;
@property (nonatomic,strong) UILabel *describe;
/*當前顯示的照片引索*/
@property (nonatomic,assign) int index;


@end

@implementation ViewController

/*
 @property
 1.創建了getter &setter 方法
 2.生成一個帶_的成員變量,直接讀取成員變量不會經過getter方法&setter方法。


 iPhone的UI控件UIImage和UIImageView有什麼區別呢?
 uiimage 是圖片,不是控件;他的父類爲NSObject;
 UIImageView是加載圖片的控件,父類爲UIView
*/
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //1.標題
    _lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 40)];
    _lable.text=@"1/5";
    _lable.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:_lable];
    //2.圖片
    CGFloat ImageW=200;
    CGFloat ImageH=200;
    CGFloat ImageX=(self.view.bounds.size.width-ImageW)*0.5;
    CGFloat ImageY= CGRectGetMaxY(self.lable.frame)+20;

    _Image=[[UIImageView alloc] initWithFrame:CGRectMake(ImageX, ImageY, ImageW, ImageH)];
    _Image.image=[UIImage imageNamed:@"biaoqingdi"];
    [self.view addSubview:_Image];
    //3.描述文字
    CGFloat describeY=CGRectGetMaxY(self.Image.frame);//注意此處不能寫成bounds因爲bounds.y一定恆等於0;
    _describe=[[UILabel alloc] initWithFrame:CGRectMake(0, describeY, self.view.bounds.size.width, 100)];
    _describe.text=@"弄啥哩!";
    _describe.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:_describe];
    //4.左邊的按鈕
    _LeftButton =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    CGFloat centerX=self.Image.frame.origin.x*0.5;
    CGFloat centerY=self.Image.center.y;
    _LeftButton.center=CGPointMake(centerX, centerY);
    [_LeftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"]forState:UIControlStateNormal];
    [_LeftButton setBackgroundImage:[UIImage imageNamed:@"left_right_highlighted"] forState:UIControlStateHighlighted];
    [self.view addSubview:_LeftButton];
    //5.右邊的按鈕
    _RightButton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    _RightButton.center=CGPointMake(self.view.bounds.size.width-centerX, centerY);
    [_RightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
    [self.view addSubview:_RightButton];
    //這樣就連線了
    [_RightButton addTarget:self action:@selector(NextMove) forControlEvents:UIControlEventTouchUpInside];




}
-(void) NextMove{
    NSLog(@"%s",__func__);//會直接打印出方法名
    self.index++;

    //設置序號
    self.lable.text=[NSString stringWithFormat:@"%d/%d", self.index+1,5];

    switch (self.index) {
        case 0:
            self.Image.image=[UIImage imageNamed:@"biaoqingdi"];
            self.describe.text=@"表情";
            break;
        case 1:
            self.Image.image=[UIImage imageNamed:@"bingli"];
            self.describe.text=@"2";
            break;
        case 2:
            self.Image.image=[UIImage imageNamed:@"chiniupa"];
            self.describe.text=@"3";
            break;
        case 3:
            self.Image.image=[UIImage imageNamed:@"danteng"];
            self.describe.text=@"danteng";
        case 4:
            self.Image.image=[UIImage imageNamed:@"wangba"];
            self.describe.text=@"wangba";
            break;

        default:
            break;
    }

    //控制按鈕狀態
    _RightButton.enabled=(self.index!=4);
}

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

@end

代碼抽取

在OC中,很多方法的第一個參數,都是觸發該方法的對象。

-(void)clickButton:(UIButton *)button{
//根據按鈕調整當前顯示圖片的索引
 self.index+=button.tag;
[self showphotoInfo];
}

_leftButton.tag=-1;
_rightButton.tag=1;

調用監聽方法

[_leftButton addTarget:self action:@selector(clickButton)] forControlEvents;
UIControlEventTouchUpInside];

接下來修改switch

NSDictionary *dict1=@{@"name":@"biaoqingdi",@"desc":@"表情"};
NSDictionary *dict2=@{@"name":@"bingli",@"desc:@"病歷"};
NSDictionary *dict3=@{@"name":@"chiniupa","desc":@"喫牛扒"};
......
NSArray *array=@[dict1,dict2,dict3,....];

//設置圖像和描述
self.image.image=[UIImage imageNamed:array[self.index][@"name"]];
self.descLabel.text=array[self.index][@"desc"];

接下來:懶加載+get方法
如果由於程序的運行順序導致運行不一樣,但是不能讓團隊的人也知道
就引入懶加載(延遲加載),通過getter實現
效果:讓對象在最需要的時候才創建。

-(NSArray *)imageList
{
if(_imageList==nil){
NSDictionary *dict1=@{@"name":@"biaoqingdi",@"desc":@"表情"};
NSDictionary *dict2=@{@"name":@"bingli",@"desc:@"病歷"};
NSDictionary *dict3=@{@"name":@"chiniupa","desc":@"喫牛扒"};
......
NSArray *array=@[dict1,dict2,dict3,....];

}return _imageList;
}

打斷點
繼續執行(程序會重新運行) 單步執行 單步進入(跟蹤一個方法內部) 單步跳出(跳出當前跟蹤方法)

plist文件

resource Property List  Plist
//”包“Bundle [NSBundle mainBundle] 編譯安裝之後對應的程序包。
NSString *path=[[NSBundle mainBundle] pathForResource:@"ImageList"
NSLog(@"%@",path);

xcode5之前的圖片不是放在Assert.car打包的

//在OC中ContentsOfFile,通常需要完整的路徑
_imageList=[NSArray arrayWithContentsOfFile:path];
NSLog(@"%@",_imageList);

UILabel源代碼

// this determines the number of lines to draw and what to do when sizeToFit is called. default value is 1 (single line). A value of 0 means no limit
// if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be
// truncated using the line break mode.

@property(nonatomic) NSInteger numberOfLines;

//需要Label具有足夠的高度,不限制顯示的行數
_descLabel.numberOfLines=0;//默認單行顯示,但是現在要改成多行

控件的懶加載

在getter方法中,不要再使用self,否則會重複調用getter方法,造成死循環
-(UILabel *)noLabel{
if(_noLabel==nil){}return _noLabel;
}
-(void)viewDidLoad{
[super viewDidLoad];
[self noLabel];
}

還有

-(UILabel *)descLabel{
if(!_descLabel){
}return _descLabel;
}也可以

swift不允許這麼寫

frame的理解?

一個view的frame包含它的矩形的長寬和他在父視圖中的座標原點(origin)x和y座標.
frame的值就是一個CGRect包含(originX,originY,width,height)
originX和originY對應着該對象在其superview中的座標,也就是說他是一個相對座標.
view的frame是view在它的superview的位置和尺寸.

UIview中bounds和frame的區別?

絕對座標(x,y是相對於原點的),相對座標
bounds是指這個view在它自己座標系的座標和大小,而frame指的是這個view在它superview的座標系的座標和大小.
主要區別在座標系這塊.
frame是相對座標,bounds是絕對座標.
很明顯bounds的原點是(0,0),而frame的原點卻是任意的.
frame如果一個按鈕,是在表格中,按鈕的frame的座標也是相對的,並不是相對屏幕,也就是說相對座標,不是絕對座標,
很明顯是一個自己爲原點的座標系,一個是以屏幕爲原點的座標系.

Strong&weak

Person.h

@property (nonatomic,copy) NSString*name
Person.m
/*ARC中dealloc主要用於調試,判斷對象是否被釋放*/
-(void)dealloc{
NSLog(@"person被釋放");
}

main.m

/*在OC中
1.如果對象沒有強引用,會被立即釋放
2.默認的對象,都是強引用的
__weak 表示弱引用
*/
int main(int argc,const char*argv[]){
@autoreleasepool{
__weak Person *p=[[Person alloc] init];
Person *p=[[Person alloc] init]};
p.name=@"zhangsan";
}
return 0;
}


@property (nonatomic,weak) Personc*person;
Person*p=[[perosn alloc] init];強引用
self.peron=p//弱引用

如果一個對象時weak引用但是一旦 self.view addsubview 看源碼subview是一個array,關聯裏面的view的是使用強引用,所以即使對象時weak被subview關聯還是不會馬上釋放

總結

  • 控件
    如果是用Storyboard 拖線,控件用weak
    如果用代碼創建界面,控件可以用strong;
  • 自定義對象.需要使用Strong
  • NSString,使用copy
  • 數字型的int,使用assign

Tom幀動畫

    //
    //  ViewController.m
    //  0812-Tom貓
    //
    //  Created by susan on 16/1/13.
    //  Copyright © 2016年 susan. All rights reserved.
    //

    #import "ViewController.h"

    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *Tom;
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [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.
    }
    - (IBAction)eatBird {
        //動畫圖片的數組
        NSMutableArray *arrayM=[NSMutableArray array];

        //添加動畫播放的圖片
        for(int i=0;i<40;i++){
            //圖像名稱
            NSString *imageName=[NSString stringWithFormat:@"eat_%02d.jpg",i];
            //注意%02d表示0開通,2爲兩位數,如果不足用0補齊

            UIImage *image=[UIImage imageNamed:imageName];
            [arrayM addObject:image];

        }
        //設置動畫數組
        self.Tom.animationImages=arrayM;
        //重複1次
        self.Tom.animationRepeatCount=1;
        //動畫時長
        self.Tom.animationDuration=self.Tom.animationImages.count*0.075;
        //開始動畫
        [self.Tom startAnimating];

    }
    - (IBAction)knockOut {

        [self toAnimationWithName:@"knockout" count:81];

    //    //動畫圖片的數組
    //    NSMutableArray *arrayM=[NSMutableArray array];
    //    
    //    //添加動畫播放的圖片
    //    for(int i=0;i<81;i++){
    //        //圖像名稱
    //        NSString *imageName=[NSString stringWithFormat:@"knockout_%02d.jpg",i];
    //        //注意%02d表示0開通,2爲兩位數,如果不足用0補齊
    //        
    //        UIImage *image=[UIImage imageNamed:imageName];
    //        [arrayM addObject:image];
    //        
    //    }
    //    //設置動畫數組
    //    self.Tom.animationImages=arrayM;
    //    //重複1次
    //    self.Tom.animationRepeatCount=1;
    //    //動畫時長
    //    self.Tom.animationDuration=self.Tom.animationImages.count*0.075;
    //    //開始動畫
    //    [self.Tom startAnimating];
    }

    // 重構代碼
    -(void)toAnimationWithName:(NSString *)name count:(NSInteger)count{
        //如果正在動畫,直接退出
        if([self.Tom isAnimating]) return;
    //動畫圖片的數組
        NSMutableArray *arrayM=[NSMutableArray array];
        //添加動畫播放的圖片
        for(int i=0;i<count;i++){
        //圖像名稱


            NSString *imageName=[NSString stringWithFormat:@"%@_%02d.jpg",name,i];
            //方法一:
            //UIImage *image=[UIImage imageNamed:imageName];

            //方法二: ContentsOfFile需要全路徑

            //tip:如果放在Image.xcassets中的圖片,不能使用imageWithContentsOfFile;
            //Images.xcassets中不要存放大的,不常用的圖片;
            NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
            UIImage *image = [UIImage imageWithContentsOfFile:path];
            [arrayM addObject:image];

        }

        //設置動畫數組
        self.Tom.animationImages=arrayM;
        //重複1次
        self.Tom.animationRepeatCount=1;
        //動畫時長
        self.Tom.animationDuration=self.Tom.animationImages.count*0.075;
        //開始動畫
        [self.Tom startAnimating];

        //動畫結束之後,清理動畫數組
        //performSelector定義在NSObject分類中
       //[self performSelector:@selector(cleanup) withObject:nil afterDelay:self.Tom.animationDuration];


        [self.Tom performSelector:@selector(setAnimationImages) withObject:nil afterDelay:self.Tom.animationDuration];



    }

-(void)cleanup{
    NSLog(@"%s",__func__);
//    self.Tom.animationImages = nil;
    //等價於
   [self.Tom setAnimationImages:nil];
}

//小技巧:
//可以設置button text clear color; tag設置爲圖片的數量

-(IBAction)TomAction:(UIButton *)sender{
//currentTitle 可以取出按鈕當前的標題文字
    [self TomAnimationWithName:sender.currentTitle count:sender.tag];


}

    @end


  • JPG:壓縮比較高,通常用於照片,網頁
    屬於有損壓縮
    壓縮使得解壓縮,對CPU消耗大,慢,費電
  • PNG:壓縮比較高
    無損壓縮
    解壓縮效率高,對CPU消耗小,蘋果推薦使用的

jpg直接放到image 導不進去
要放到supportFile

ImageView的方法
- (void)startAnimating;
- (void)stopAnimating;
- (BOOL)isAnimating;

關於圖像的實例化
UIImage是一個圖像 UIImageView是顯示圖像的相框
imageName:系統推薦使用,但是圖像實例化之後的釋放是由系統負責.
但是,如果要自己釋放,不能使用imageNamed方法.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章