让图片左右缓慢移动的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(, , 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的更多相关文章
- 用Javascript实现图片的缓慢缩放效果
<body> <!--页面布局:一张图片两个按钮--> <div style = "width:400px;margin:0 auto"> &l ...
- CSS3与页面布局学习总结(五)——Web Font与Sprite
一.web font web font是应用在web中的一种字体技术,在CSS中使用font-face定义新的字体.先了解操作系统中的字体: a).安装好操作系统后,会默认安装一些字体,这些字体文件描 ...
- iPhone开发中的技巧整理
1.NSCalendar用法 -(NSString *) getWeek:(NSDate *)d { NSCalendar *calendar = [[NSCalendar alloc] initWi ...
- 仿windows phone风格主界面
使用了ZAKER到最新版本,其主界面采用windows phone的风格,感觉还蛮好看的,挺喜欢的,就模仿写了一下,实现到界面截图如下: 第一版面: 第二版面: 在实现了它到九宫格菜单,还实现了背景图 ...
- [ios2] 开发技巧【转】
1.NSCalendar用法 -(NSString *) getWeek:(NSDate *)d { NSCalendar *calendar = [[NSCalendar alloc] initWi ...
- 封装了一个电商放大镜移入放大的功能,适用于VUE
代码地址:https://github.com/zhongqiulan/jqimgzoom 由于vue只支持ie9以上版本,所以这个插件也是一样的 效果图: 第一步,在goodsinfo文件中引入cs ...
- 如何用字体在网页中画icon
一.用css雪碧图 1.简介 CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许将一个页面涉及到的所有零星图片都包含到一张大图中, 利用CSS的“background- ...
- VS2015--win32project配置的一些想法之在 Visual Studio 2015 中进行调试的同一时候分析性能
出处: https://msdn.microsoft.com/zh-cn/magazine/dn973013(en-us).aspx 很多开发商花了绝大多数时间获取应用程序才干正常发挥作用.更少的时间 ...
- iphone开发技巧整合
1.NSCalendar用法 -(NSString *) getWeek:(NSDate *)d { NSCalendar *calendar = [[NSCalendar alloc] initWi ...
随机推荐
- GitHub与Git指令入门
GitHub入门 创建账户 之前做代码版本控制都是在bitbucket上面的私有仓库.现在开源社区挺火的,想在github找些开源的工程学习一下,于是加入了github,这里记录一下入门的经历. 首先 ...
- Eclipse/MyEclipse如何快速提取变量(最强帮手)
不多说,直接上干货! Eclipse里如何快速提取变量? 按alt+shift+l MyEclipse里如何快速提取变量? 按alt+shift+l 成功!快速提取变量 扩展学习 Eclipse/My ...
- Table '.\gts\eventdata#P#p0' is marked as crashed and last (automatic?) repair failed
修复数据表操 MYSQL数据表出现问题,提示:Error: Table './db_name/table_name' is marked as crashed and last (automatic? ...
- linux ssh 免密码登录的配置过程
# ssh-keygen -t rsa -C "自定义描述" -f ~/.ssh/自定义生成的rsa文件 # cd ./.ssh # touch config # 粘贴 Host ...
- 转:详解PV、UV、VV、IP及其关系与计算
一.什么是PV? PV即Page View,网站浏览量,指页面浏览的次数,用以衡量网站用户访问的网页数量.用户每次打开一个页面便记录1次PV,多次打开同一页面则浏览量累计.一般来说,PV与来访者的数量 ...
- [转]Redis 数据类型
本文转自:http://www.runoob.com/redis/redis-data-types.html Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),s ...
- 使用PowerShell创建SSAS Role
PowerShell, SSAS, Role, DatabasePermission, Cube, Dimension, CubePermission, CubeDimensionCube 在SSAS ...
- Swift函数_外部参数名,
//1________________ /** *没使用外部参数名的函数 */ func inputScore(name:String,score1:Int,score2:Int) { } /** * ...
- [PHP] PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1)
PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1): 1.bench.php 可在PHP源代码的 php-src/Zend 目录 2.micro_bench.php 也可以在 PHP ...
- windows emacs 中拷贝文件
cp d:/workspace/LoginWeb/target/LoginWeb.war D:/Program\ Files/apache-tomcat-7.0.78/webapps/LoginWeb ...