让图片左右缓慢移动的MoveView

让图片左右缓慢移动的MoveView

效果:

源码:

MoveView.h 与 MoveView.m

//
//  MoveView.h
//  AnimationView
//
//  Created by XianMingYou on 15/1/28.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
    MV_RIGHT = 0x19871220, // 开始时候向右移动
    MV_LEFT,               // 开始时候向左移动
} EStartMoveDirection;

@interface MoveView : UIView

@property (nonatomic) CGFloat               animationDuration; // 移动动画持续的时间
@property (nonatomic) EStartMoveDirection   direction;         // 起始移动的方向(默认值向右)
@property (nonatomic, strong) UIImage      *image;             // 输入的图片

/**
 *  创建出view
 */
- (void)buildView;

/**
 *  开始动画
 */
- (void)doAnimation;

/**
 *  透明百分比
 *
 *  @param percent 百分比(0 ~ 1)
 */
- (void)alphaPercent:(CGFloat)percent;

@end
//
//  MoveView.m
//  AnimationView
//
//  Created by XianMingYou on 15/1/28.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "MoveView.h"

@interface MoveView ()

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic) CGRect  startFrame; // 起始frame值
@property (nonatomic) CGRect  endFrame;   // 结束frame值

@end

@implementation MoveView

- (void)buildView {
    // 添加遮罩
    self.layer.masksToBounds = YES;
    
    // 如果没有图片,则直接退出
    if (self.image == nil) {
        return;
    }

    // 获取图片高度
    CGFloat height         = self.frame.size.height;
    CGSize imageSize       = self.image.size;
    CGFloat imageViewWidth = height / imageSize.height * imageSize.width;
    
    // 获取到了尺寸
    self.imageView       = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageViewWidth, height)];
    self.imageView.image = self.image;
    
    // 获取初始尺寸
    self.startFrame = self.imageView.frame;
    self.endFrame   = CGRectMake(self.frame.size.width - self.imageView.frame.size.width, 0, imageViewWidth, height);
    
    [self addSubview:self.imageView];
}

- (void)doAnimation {
    // 获取默认值
    if (self.direction != MV_RIGHT && self.direction != MV_LEFT) {
        self.direction = MV_RIGHT;
    }
    
    if (self.direction == MV_RIGHT) {
        self.imageView.frame = self.startFrame;
    } else {
        self.imageView.frame = self.endFrame;
    }
    
    // 获取动画时间
    self.animationDuration = (self.animationDuration <= 0 ? 10.f : self.animationDuration);
    
    // 开始动画
    [self startAnimation];
}

- (void)alphaPercent:(CGFloat)percent {
    self.alpha = percent;
}

- (void)startAnimation {
    
    if (self.direction == MV_RIGHT) {
        [UIView animateWithDuration:self.animationDuration
                         animations:^{
                             self.imageView.frame = self.endFrame;
                         } completion:^(BOOL finished) {
                             [UIView animateWithDuration:self.animationDuration
                                              animations:^{
                                                  self.imageView.frame = self.startFrame;
                                              } completion:^(BOOL finished) {
                                                  [self startAnimation];
                                              }];
                         }];
    } else {
        [UIView animateWithDuration:self.animationDuration
                         animations:^{
                             self.imageView.frame = self.startFrame;
                         } completion:^(BOOL finished) {
                             [UIView animateWithDuration:self.animationDuration
                                              animations:^{
                                                  self.imageView.frame = self.endFrame;
                                              } completion:^(BOOL finished) {
                                                  [self startAnimation];
                                              }];
                         }];
    }
}

@end

控制器源码:

//
//  ViewController.m
//  MoveView
//
//  Created by YouXianMing on 15/1/28.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "MoveView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 背景移动的图
    MoveView *moveView         = [[MoveView alloc] initWithFrame:self.view.bounds];
    moveView.image             = [UIImage imageNamed:@"1.jpg"];
    moveView.animationDuration = 15;
    moveView.direction         = MV_RIGHT;
    [moveView buildView];
    [moveView doAnimation];
    
    [self.view addSubview:moveView];
}

@end

核心要点:

原文地址:https://www.cnblogs.com/YouXianMing/p/4257078.html