让图片左右缓慢移动的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(, , imageViewWidth, height)];
self.imageView.image = self.image; // 获取初始尺寸
self.startFrame = self.imageView.frame;
self.endFrame = CGRectMake(self.frame.size.width - self.imageView.frame.size.width, , 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 <= ? .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 = ;
moveView.direction = MV_RIGHT;
[moveView buildView];
[moveView doAnimation]; [self.view addSubview:moveView];
} @end

核心要点:

让图片左右缓慢移动的MoveView的更多相关文章

  1. 用Javascript实现图片的缓慢缩放效果

    <body> <!--页面布局:一张图片两个按钮--> <div style = "width:400px;margin:0 auto"> &l ...

  2. CSS3与页面布局学习总结(五)——Web Font与Sprite

    一.web font web font是应用在web中的一种字体技术,在CSS中使用font-face定义新的字体.先了解操作系统中的字体: a).安装好操作系统后,会默认安装一些字体,这些字体文件描 ...

  3. iPhone开发中的技巧整理

    1.NSCalendar用法 -(NSString *) getWeek:(NSDate *)d { NSCalendar *calendar = [[NSCalendar alloc] initWi ...

  4. 仿windows phone风格主界面

    使用了ZAKER到最新版本,其主界面采用windows phone的风格,感觉还蛮好看的,挺喜欢的,就模仿写了一下,实现到界面截图如下: 第一版面: 第二版面: 在实现了它到九宫格菜单,还实现了背景图 ...

  5. [ios2] 开发技巧【转】

    1.NSCalendar用法 -(NSString *) getWeek:(NSDate *)d { NSCalendar *calendar = [[NSCalendar alloc] initWi ...

  6. 封装了一个电商放大镜移入放大的功能,适用于VUE

    代码地址:https://github.com/zhongqiulan/jqimgzoom 由于vue只支持ie9以上版本,所以这个插件也是一样的 效果图: 第一步,在goodsinfo文件中引入cs ...

  7. 如何用字体在网页中画icon

    一.用css雪碧图 1.简介 CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许将一个页面涉及到的所有零星图片都包含到一张大图中, 利用CSS的“background- ...

  8. VS2015--win32project配置的一些想法之在 Visual Studio 2015 中进行调试的同一时候分析性能

    出处: https://msdn.microsoft.com/zh-cn/magazine/dn973013(en-us).aspx 很多开发商花了绝大多数时间获取应用程序才干正常发挥作用.更少的时间 ...

  9. iphone开发技巧整合

    1.NSCalendar用法 -(NSString *) getWeek:(NSDate *)d { NSCalendar *calendar = [[NSCalendar alloc] initWi ...

随机推荐

  1. hibernate_annotation字段映射位置

    @Id private int id; private String name; private String wifeName; private Date birthDate; //Title是En ...

  2. Java PrepareStatement

    1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程2.使用 Statement 对象.在对数据库只执行一次性存取的时侯,用 Statement 对象 ...

  3. 在Linux上使用C语言编程获取IPv4地址及子网掩码

    在Linux上(如Ubuntu或CentOS), 获取某个Network Interface比如eth0的IP地址等信息,我们可以使用ifconfig或者ip addr show命令. $ ifcon ...

  4. Hibernate 4.3 SessionFactory

    Configuration configuration = new Configuration().configure(); //以下这两句就是4.3的新用法 StandardServiceRegis ...

  5. mysql-5.6.31安装(单实例 Linux)

    安装版本:mysql-5.6.31 安装环境:Linux RHEL6.5.x86 安装要求:单实例,端口为默认:3306              (1) 要求安装Mysql数据库版本号及包名为:my ...

  6. 关于svn插件突然失效问题

    这个分享一下 安装  MyBatisGenerator 插件 之后,svn失效,删掉mybatis 后,svn就恢复正常...这怎么割 一翻折腾无效,后来发现  MyBatisGenerator  和 ...

  7. java调用ruby代码

    问题: 最近在做一个应用的时候碰到了一个问题.客户端需要调用服务器端传回的脚本信息,然后执行.其中脚本类型包括ruby.而java中调用ruby的代码大致如下: String jrubyCode=&q ...

  8. 利用jquery的ajax实现跨域,内部其实是jsonp协议了,不是XHRhttp协议

    一.同源策略 要理解跨域,先要了解一下“同源策略”.所谓同源是指,域名,协议,端口相同.所谓“同源策略“,简单的说就是基于安全考虑,当前域不能访问其他域的东西. 一些常见的是否同源示例可参照下表: 在 ...

  9. 【OpenCV】邻域滤波:方框、高斯、中值、双边滤波

    原文:http://blog.csdn.net/xiaowei_cqu/article/details/7785365 邻域滤波(卷积)   邻域算子值利用给定像素周围像素的值决定此像素的最终输出.如 ...

  10. 获取服务器时间ajax

    $.ajax({ type:"OPTIONS", url:"/", complete:function(x){ // alert(x.getResponseHe ...