让图片左右缓慢移动的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. java的instanceof简单使用

    instanceof:是java中用来判断一个对象属于哪个类型的关键字 (instanceof是instance和of两个单词组成,但of并没有大写) eg: public class Test{   ...

  2. Python基础(10) - 异常

    Python 异常:程序出现了错误而在正常控制流以外采取的行为 Python中常见的异常: 1. NameError:尝试访问一个未声明的变量 >>> something Trace ...

  3. JAVA练手--链表

    package tet; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; impo ...

  4. MVC缓存(一)

    //OutputCache是设置缓存,参数Duration设置缓存的过期时间,OutputCache可以加到Controller上,也可以加到Action上,但是当Controller与Action都 ...

  5. [C#]LockBits使用笔记

    昨天想基于一张图片做个手机锁屏来着,原图如下:主要是嫌白底太丑了,一开始是想画图工具直接油漆桶伺候,然而一浇上去就发现问题了,变成了这样:看来得手工处理一下把底色统一了,原图分辨率挺高的,SetPix ...

  6. LeetCode刷题第一天

    1 . 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用 ...

  7. 一个JNI的helloworld小demo

    最近想学习一下jni,在网上看了一些demo,自己也操作了一遍,首先我将我自己学习的demo网站贴出来:https://blog.csdn.net/lwcloud/article/details/78 ...

  8. Springboot @ResponseBody返回中文乱码

    最近我在把Spring 项目改造Springboot,遇到一个问题@ResponseBody返回中文乱码,因为response返回的content-type一直是application/json;ch ...

  9. tensorflow(一):图片处理

    一.图片处理 1.图片存取 tf.gfile import tensorflow as tf import matplotlib.pyplot as plt image_bytes = tf.gfil ...

  10. java poi处理新版xlsx后缀的excel

    jar包下载地址http://poi.apache.org/download.html#POI-3.17,我把下载文件中的所有jar包都导入项目中才能跑 参考以下博客 https://www.cnbl ...