IOS(UI)_相框動畫(動畫輪播)

這裏寫圖片描述

主要的UI佈局就不多說了

相框動畫主要使用動畫效果來輪播

  • 1.向創建一個UIImage把圖片給他(anglaybaby.jpg爲圖片初始化是顯示的圖片)
    UIImage *image=[UIImage imageNamed:@"anglaybaby.jpg"];
  • 2.創建UIImageView把UIImage放進創建好的UIImageView(並設置UIImageView的大小)
    UIImageView *imageView=[[UIImageView alloc] initWithImage:image];
  • 3.利用UIImageView來製作動畫
        imageView.animationImages=@[
        [UIImage imageNamed:@"anglaybaby"],
        [UIImage imageNamed:@"anglaybaby2"],
        [UIImage imageNamed:@"anglaybaby3"],
        [UIImage imageNamed:@"anglaybaby4"]];
    //運行完成的時間長短
    imageView.animationDuration=3.0;
    //循環次數
    imageView.animationRepeatCount=FLT_MAX;//無限循環
  • 4.動畫停止和開始
        [imageView startAnimating];//開始

        [imageView stopAnimating];//停止

實現代碼:
ViewController.m


#import "ViewController.h"
#import "NextViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //用
    //UIImageView來表示相框,用來承載圖片

    UIImage *image=[UIImage imageNamed:@"anglaybaby.jpg"];

    //UIImage *image=[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"anglaybaby" ofType:@"jpg"]];//ofType爲後綴名如果爲png就不用寫
    NSLog(@"image.size=%@",[NSValue valueWithCGSize:image.size]);

    UIImageView *imageView=[[UIImageView alloc] initWithImage:image];

    imageView.backgroundColor=COLOR(34, 199, 250, 1);
    //添加到視圖上

    imageView.translatesAutoresizingMaskIntoConstraints=NO;
    [self.view addSubview:imageView];

    NSLayoutConstraint *constraint5=[NSLayoutConstraint constraintWithItem:imageView
                                                                 attribute:NSLayoutAttributeCenterX
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.view
                                                                 attribute:NSLayoutAttributeCenterX
                                                                multiplier:1
                                                                  constant:0];
    [self.view addConstraint:constraint5];

    NSLayoutConstraint *constraint6=[NSLayoutConstraint constraintWithItem:imageView
                                                                 attribute:NSLayoutAttributeCenterY
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.view
                                                                 attribute:NSLayoutAttributeCenterY
                                                                multiplier:1
                                                                  constant:0];

    [self.view addConstraint:constraint6];
    NSLayoutConstraint *constraint7=[NSLayoutConstraint constraintWithItem:imageView
                                                                 attribute:NSLayoutAttributeWidth
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:nil
                                                                 attribute:NSLayoutAttributeWidth
                                                                multiplier:1
                                                                  constant:200];
    [self.view addConstraint:constraint7];

    NSLayoutConstraint *constraint8=[NSLayoutConstraint constraintWithItem:imageView
                                                                 attribute:NSLayoutAttributeHeight
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:nil
                                                                 attribute:NSLayoutAttributeHeight
                                                                multiplier:1
                                                                  constant:200];
    [self.view addConstraint:constraint8];


    //利用image製作相框動畫
    imageView.animationImages=@[[UIImage imageNamed:@"anglaybaby"],
                                [UIImage imageNamed:@"anglaybaby2"],
                                [UIImage imageNamed:@"anglaybaby3"],
                                [UIImage imageNamed:@"anglaybaby4"]];

    imageView.animationDuration=3.0;

    imageView.animationRepeatCount=FLT_MAX;//無限循環

    imageView.tag=1000;

    //等比

    /*
     相框填充方式


     UIViewContentModeScaleToFill,//拉伸自動適應
     UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
     UIViewContentModeScaleAspectFill,    按原始大小顯示 // contents scaled to fill with fixed aspect. some portion of content may be clipped.
     UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
     UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
     UIViewContentModeTop,
     UIViewContentModeBottom,
     UIViewContentModeLeft,
     UIViewContentModeRight,
     UIViewContentModeTopLeft,
     UIViewContentModeTopRight,
     UIViewContentModeBottomLeft,
     UIViewContentModeBottomRight,


     */
    imageView.contentMode=UIViewContentModeScaleAspectFit;


    //圖片下面
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setTitle:@"開始" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button2 setTitle:@"停止" forState:UIControlStateSelected];
    button2.backgroundColor=COLOR(34, 199, 250, 1);
    button2.layer.cornerRadius=5.0;
    button2.layer.masksToBounds=YES;
    [button2 addTarget:self action:@selector(animatiomAction:) forControlEvents:UIControlEventTouchUpInside];
    button2.translatesAutoresizingMaskIntoConstraints=NO;
    [self.view addSubview:button2];


    NSLayoutConstraint *constraint_button1=[NSLayoutConstraint constraintWithItem:button2
                                                                 attribute:NSLayoutAttributeCenterX
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.view
                                                                 attribute:NSLayoutAttributeCenterX
                                                                multiplier:1
                                                                  constant:0];
    [self.view addConstraint:constraint_button1];

    NSLayoutConstraint *constraint_button2=[NSLayoutConstraint constraintWithItem:button2
                                                                 attribute:NSLayoutAttributeTop
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:imageView
                                                                 attribute:NSLayoutAttributeBottom
                                                                multiplier:1
                                                                  constant:20];

    [self.view addConstraint:constraint_button2];
    NSLayoutConstraint *constraint_button3=[NSLayoutConstraint constraintWithItem:button2
                                                                 attribute:NSLayoutAttributeWidth
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:nil
                                                                 attribute:NSLayoutAttributeWidth
                                                                multiplier:1
                                                                  constant:100];
    [self.view addConstraint:constraint_button3];

    NSLayoutConstraint *constraint_button4=[NSLayoutConstraint constraintWithItem:button2
                                                                 attribute:NSLayoutAttributeHeight
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:nil
                                                                 attribute:NSLayoutAttributeHeight
                                                                multiplier:1
                                                                  constant:30];
    [self.view addConstraint:constraint_button4];






}


-(void)animatiomAction:(UIButton *)sender
{
    UIImageView *imageView=(UIImageView *)[self.view viewWithTag:1000];
    sender.selected=!sender.selected;
    if (sender.selected)
    {
        [imageView startAnimating];
    }
    else
    {
        [imageView stopAnimating];
    }
}

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

@end

這裏寫圖片描述

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