让图片左右缓慢移动的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 ...
随机推荐
- java处理数据库的CRUD
package com.lhy.jdbc.util; import java.sql.Connection; import java.sql.PreparedStatement; import jav ...
- window下eclipse安装python插件
1.安装python环境 python安装包下载地址:https://www.python.org/downloads/windows/ 2.在eclipse中在线安装PyDev插件 启动Eclips ...
- android 签名验证防止重打包
网上资料很多,这里只做一个笔记反编译 dex 修改重新打包签名后 apk 的签名信息肯定会改变,所以可以在代码中判断签名信息是否被改变过,如果签名不一致就退出程序,以防止 apk 被重新打包. 1 j ...
- mac环境下使用docker安装nginx
前言 距离上一篇文章已经很长时间,近期实在事情太多了,也没来得及继续更新一些新的内容.现在开发使用的工作实在太多了,小编实在忍受不了windows那样卡机的状态,于是最近换了一个mac电脑,虽然做开发 ...
- mysql去除重复记录案例
例1,表中有主键(可唯一标识的字段),且该字段为数字类型 1 测试数据 /* 表结构 */ DROP TABLE IF EXISTS `t1`; CREATE TABLE IF NOT EXISTS ...
- Pnel控件
分组类控件 面板控件(Panel) 分组框控件(GroupBox) 选项卡控件(TabControl)等控件 Panel 控件是由System.Windows.Forms.Panel类提供的,主要 ...
- ORB-SLAM2实现(kinect V1/ROS)
实验室电脑环境ubuntu14.04和ROS indigo已经装好. 1. 构建工作空间[非常重要的一步] mkdir -p ~/catkin_ws/src cd ~/catkin_ws/ catki ...
- JMM和底层实现原理
- 找到链表中倒数第k个数
本文来源于翁舒航的博客,点击即可跳转原文观看!!!(被转载或者拷贝走的内容可能缺失图片.视频等原文的内容) 若网站将链接屏蔽,可直接拷贝原文链接到地址栏跳转观看,原文链接:https://www.cn ...
- Linux下ipv6配置系列
Linux下ipv6配置系列一:如何配置Linux系统ipv6环境 Linux下ipv6配置系列二:如何为Nginx添加ipv6模块 Linux下ipv6配置系列三:如何为Nginx配置IPv6端口监 ...