1.新建empty AppLication,添加HomeViewController页面, iphone.png图片

2.在 HomeViewController.xib中添加Image View,并调整其大小;再添加一个Slider控件

3.HomeViewController.h代码:

#import <UIKit/UIKit.h>

 
@interface HomeViewController : UIViewController{
 
    CGPoint delta;//坐标变化量
    NSTimer *timer;
    CGSize picSize;//图片大小
    
}
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
@property (retain, nonatomic) IBOutlet UISlider *slider;
 
- (IBAction)sliderChanged:(id)sender;
 
 
@end

HomeViewController.m代码:

#import "HomeViewController.h"

 
@interface HomeViewController ()
 
@end
 
@implementation HomeViewController
@synthesize imageView;
@synthesize slider;
 
 
- (void)move{
 
    imageView.center = CGPointMake(imageView.center.x + delta.x, 
                                   imageView.center.y + delta.y);
    
    if (imageView.center.x > self.view.bounds.size.width - picSize.width / 2 || 
        imageView.center.x < picSize.width / 2) {
        delta.x = -delta.x;
    }
    
    if (imageView.center.y >self.view.bounds.size.height - picSize.height / 2 ||
        imageView.center.y < picSize.height / 2) {
        delta.y = -delta.y;
    }
    /*我们不断地改变imaageView的center坐标,横向和纵向的调整数值为delta的x和y值,当检测到imageView的位置
     超过或小于屏幕的宽度或者高度的时候,我们改变其运动的方向*/
}
 
 
- (void)viewDidLoad
{
    picSize = imageView.bounds.size;
    delta = CGPointMake(8.0, 4.0);
    timer = [NSTimer scheduledTimerWithTimeInterval:slider.value
                                             target:self 
                                           selector:@selector(move) 
                                           userInfo:nil 
                                            repeats:YES];
    
    [super viewDidLoad];
 
}
 
 
- (void)dealloc {
    [timer invalidate];
    [imageView release];
    [slider release];
    [super dealloc];
}
- (IBAction)sliderChanged:(id)sender {
    [timer invalidate];
    timer = [NSTimer scheduledTimerWithTimeInterval:slider.value
                                             target:self 
                                           selector:@selector(move) 
                                           userInfo:nil
                                            repeats:YES];
}
@end

因为图片是静态的所以看不到效果,这个很好玩,iphone图片会在手机内四周不同位置移动,速度可以调节

                                                        二、视觉效果动画 

让动画产生平滑的感觉:

修改move方法

- (void)move{
    
    [UIView beginAnimations:@"myAnimation" context:nil];
    
    [UIView setAnimationDuration:slider.value];//动画执行时间
    
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];//设置动画的执行速度
    /*setAnimationCurve有四种常量:
            UIViewAnimationCurveLinear 在执行动画的时间内,速度始终保持如一。
            UIViewAnimationCurveEaseInOut 执行动画的时候,速度开始慢,然后加速,结束时再次变慢
            UIViewAnimationCurveEaseIn 速度开始慢,然后逐渐加速直到结束
            UIViewAnimationCurveEaseOut 速度开始快,然后逐渐减速直到结束*/
 
    imageView.center = CGPointMake(imageView.center.x + delta.x, 
                                   imageView.center.y + delta.y);
    
    [UIView commitAnimations];//动画块结束时调用,此时imageView从一个点移动到另一个点就会非常平滑,而不是让人感觉从一个点跳到了另一个点
    
    if (imageView.center.x > self.view.bounds.size.width - picSize.width / 2 || 
        imageView.center.x < picSize.width / 2) {
        delta.x = -delta.x;
    }
    
    if (imageView.center.y >self.view.bounds.size.height - picSize.height / 2 ||
        imageView.center.y < picSize.height / 2) {
        delta.y = -delta.y;
    }
    /*我们不断地改变imaageView的center坐标,横向和纵向的调整数值为delta的x和y值,当检测到imageView的位置
     超过或小于屏幕的宽度或者高度的时候,我们改变其运动的方向*/
}
 

三、视图变形

虽然可以使用NSTimer达到view位置移动的动画效果,但是也可以使用ios sdk提供的另外一种技术--Transforms(定义在Core Graphics框架中)来实现同样的效果,并且实现更多功能。

包括:Translation, 移动view的原点坐标到指定的位置;Rotation, 旋转view到一个指定的角度;Scaling, 缩放view到一个指定的宽高比例。

1、位移动画

修改HomeViewController.h

#import <UIKit/UIKit.h>
 
@interface HomeViewController : UIViewController{
 
    CGPoint delta;
    NSTimer *timer;
    CGSize picSize;
    CGPoint translation;
    
}
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
@property (retain, nonatomic) IBOutlet UISlider *slider;
 
- (IBAction)sliderChanged:(id)sender;
 

@end

修改HomeViewController.m 的move方法和viewDidLoad方法

- (void)move{
    
    imageView.transform = CGAffineTransformMakeTranslation(translation.x, translation.y);//移动view到指定的位置
    translation.x += delta.x;
    translation.y += delta.y;
    
    if (imageView.center.x + translation.x > self.view.bounds.size.width - picSize.width / 2 ||
        imageView.center.x + translation.x < picSize.width / 2) {
        delta.x = -delta.x;
    }
    
    if (imageView.center.y + translation.y > self.view.bounds.size.height - picSize.height / 2 ||
        imageView.center.y + translation.y < picSize.height / 2) {
        delta.y = -delta.y;
    }
}
 

- (void)viewDidLoad

{
    picSize = imageView.bounds.size;
    delta = CGPointMake(8.0, 4.0);
    
    translation = CGPointMake(0.0, 0.0);
    
    timer = [NSTimer scheduledTimerWithTimeInterval:slider.value
                                             target:self 
                                           selector:@selector(move) 
                                           userInfo:nil 
                                            repeats:YES];
    
    [super viewDidLoad];
 
}
 

2、旋转动画

修改HomeViewController.h

#import <UIKit/UIKit.h>
 
@interface HomeViewController : UIViewController{
 
   
    CGPoint delta;
    NSTimer *timer;
    CGSize picSize;
    CGPoint translation;
    float angle;//旋转角度
 
    
}
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
@property (retain, nonatomic) IBOutlet UISlider *slider;
 
- (IBAction)sliderChanged:(id)sender;
 

@end

修改HomeViewController.m 的move方法和viewDidLoad方法

 
- (void)move{
    imageView.transform = CGAffineTransformMakeRotation(angle);
    angle += 0.02;
    /*判断角度是否大于360º,如果大于2π(3.14159 * 2 = 6.28318),则让angle为0*/
    if (angle > 6.2832) {
        angle = 0;
    }
 
}
 

- (void)viewDidLoad

{
    picSize = imageView.bounds.size;
    delta = CGPointMake(8.0, 4.0);
    translation = CGPointMake(0.0, 0.0);

angle= 0;

    timer = [NSTimer scheduledTimerWithTimeInterval:slider.value
                                             target:self 
                                           selector:@selector(move) 
                                           userInfo:nil 
                                            repeats:YES];
    
    [super viewDidLoad];
 
}
 

 

3、缩放动画 

只需要修改slider的事件sliderChanged 即可

- (IBAction)sliderChanged:(id)sender {
 
    imageView.transform = CGAffineTransformMakeScale(slider.value, slider.value);//缩放

}

 

本文转载至 http://www.cnblogs.com/hanjun/archive/2012/10/29/2745437.html

使用NSTimer实现动画的更多相关文章

  1. iOS 分析一个支持GIF的UIImage扩展:SwiftGIF

    Github:https://github.com/bahlo/SwiftGif 这个extension代码不多,主要通过Apple的ImageIO框架进行解析GIF. 整个扩展最核心还是下面的函数, ...

  2. ios二维码扫描

    1.添加AVFoundation.framework框架 2,控制器中实现 //第一步添加AVFoundation.framework框架 #import "ViewController.h ...

  3. 16-UIKit(AutoLayout、Animation)

    目录: 一.AutoLayout自动布局 二.动画(Animation) 回到顶部 一.AutoLayout自动布局 1.什么是AutoLayout 从ios6开始引入的新技术,是新版的自动布局技术 ...

  4. iOS加载Gif图片的N种方式 By-H罗

    1.系统UIImageView 多张图片组成动画 /** * UIImageView 动画 * Memory-23M */ -(void)gifPlay1 { // NSArray *array=@[ ...

  5. Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用

    OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...

  6. 利用CAReplicatorLayer实现的加载动画

    在上一篇中,笔者简要介绍了CAReplicatorLayer,在本篇中,将介绍具体的实用价值. 实用CAReplicatorLayer作为核心技术实现加载动画. 首先,创建一个UIView的子类 @i ...

  7. 李洪强iOS经典面试题143-绘图与动画

    李洪强iOS经典面试题143-绘图与动画   绘图与动画 CAAnimation的层级结构 CAPropertyAnimation是CAAnimation的子类,也是个抽象类,要想创建动画对象,应该使 ...

  8. 动画黄金搭档:CADisplayLink&CAShapeLayer

    我们在开发中有时会遇到一些看似非常复杂的动画,不知该如何下手,今天的这篇文章中我会讲到如何利用CADisplayLink和CAShapeLayer来构建一些复杂的动画,希望能在你下次构建动画中,给你一 ...

  9. 动画黄金搭档:CADisplayLink & CAShapeLayer

    我们在开发中有时会遇到一些看似非常复杂的动画,不知该如何下手,今天的这篇文章中我会讲到如何利用CADisplayLink和CAShapeLayer来构建一些复杂的动画,希望能在你下次构建动画中,给你一 ...

随机推荐

  1. java 关于同步异步的理解

    经常看到介绍 ArrayList 和HashMap是异步,Vector和HashTable是同步,这里同步是线程安全的,异步不是线程安全的,举例说明: 当创建一个Vector对象时候, Vector ...

  2. spark sql 入门

    package cn.my.sparksql import cn.my.sparkStream.LogLevel import org.apache.spark.{SparkConf, SparkCo ...

  3. C# 异常类型

    Exception 类  描述 SystemException 其他用户可处理的异常的基本类 ArgumentException 方法的参数是非法的 ArgumentNullException 一个空 ...

  4. hibernate不调用save也保存上了

    List<Instrument> insts = instService.search(search); if (insts.size() == 1) { Instrument inst ...

  5. FATAL ERROR: Could not find ./bin/my_print_defaults 解决方法

    FATAL ERROR: Could not find ./bin/my_print_defaults If you compiled from source, you need to run 'ma ...

  6. js导出execl

    var idTmr; function ExportExcel(tableid) {//整个表格拷贝到EXCEL中 var curTbl = document.getElementById(table ...

  7. Kafka Tools

    参考, https://cwiki.apache.org/confluence/display/KAFKA/System+Tools https://cwiki.apache.org/confluen ...

  8. php 不依赖数据实现删除图片,核心代码

    <?php $file = "ueditor\php\upload\image\*\*.png"; foreach (glob("$file") as $ ...

  9. 关于Jsp中的三种弹框

    对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3:一个带输入的对话框,可以返回用户填入的字 ...

  10. strcpy、strncpy、memcpy的区别

    一.strcpy.strncpy区别 struct gpInfo { char gpcode[9]; char gpName[50]; }; string gpstr = "SZ000001 ...