让图片左右缓慢移动的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 ...
随机推荐
- 用Akka构建一个简易的分布式文件系统
本来初期打算用Hadoop 2,可是后来有限的服务器部署了Solr Cloud,各种站点,发现资源不够了,近10T的文件,已经几乎把服务器的磁盘全部用光.想来想去,由于目前架构基于Scala的,所以还 ...
- 【es6】变量解构赋值
1.数组解构赋值 let [a,b,c]=[1,2,3];//数组解构赋值,注意:左右两边格式需一致 let [a,b]=[1,2,3];//不完全解构,取位置靠前的值 let [a=1,b]=[un ...
- python-thread封装类创建线程
#!/usr/bin/python #coding=utf-8 from time import ctime,sleep import threading class Mythead(threadin ...
- call()和apply()方法(切换上下文)
call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. apply方法: 语法:apply ...
- class对象存储
当加载一个类完成后,会在内存中实例化一个java.lang.Class类的对象,也就是该类的类对象.但是并没有明确规定必须在java堆中存放该类对象,对于HotSpot虚拟机而言,类对象存放在方法区里 ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- ayer.prompt 怎样让输入值为空也可以向下执行
http://fly.layui.com/jie/4227/ layer.prompt({title: '输入任何口令,并确认',formType: 1, //prompt风格,支持0-2value: ...
- 数据结构(三)--- B树(B-Tree)
文章图片代码来自邓俊辉老师的课件 概述 上图就是 B-Tree 的结构,可以看到这棵树和二叉树有点不同---"又矮又肥".同时子节点可以有若干个小的子节点构成.那么这样一棵树 ...
- Docker学习之Centos7下安装
Docker学习之Centos7下安装 centos7 64下直接使用yum安装docker环境,步骤如下: 卸载旧版本docker sudo yum remove docker docker-com ...
- EF fluent API如何配置主键不自动增长
在Dbcontext中作如下添加: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilde ...