zoukankan      html  css  js  c++  java
  • Add a Wiggle

    This will be a continuation of Multiple Objects and UITouch

    If you do a long touch in the main section of the iPhone on a icon you will notice the icons will wiggle. This shows the user they can be deleted or moved around. So lets try to mimic that as best we can and when we stop a drag of our image it will wiggle for two seconds.

    The first thing we need to do is import a framework to our project. So right click on the Frameworks tab. Then click on Add Framework. Then click QuartzCore FramwWork.

    So first open up touchViewController.h and we need to add in two function

    -(void)doWiggle:(UIView *)touchView;
    -(void)endWiggle:(NSTimer*)timer;
    

    Now open up touchViewController.m and we need to add in two functions

    -(void)doWiggle:(UIView *)touchView {
    
    	// grabbing the layer of the tocuhed view.
    	CALayer *touchedLayer = [touchView layer];
    
    	// here is an example wiggle
    	CABasicAnimation *wiggle = [CABasicAnimation animationWithKeyPath:@"transform"];
    	wiggle.duration = 0.1;
    	wiggle.repeatCount = 1e100f;
    	wiggle.autoreverses = YES;
    	wiggle.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(touchedLayer.transform,0.1, 0.0 ,1.0 ,2.0)];
    
    	// doing the wiggle
    	[touchedLayer addAnimation:wiggle forKey:@"wiggle"];
    
    	// setting a timer to remove the layer
    	NSTimer *wiggleTimer = [NSTimer scheduledTimerWithTimeInterval:(2) target:self selector:@selector(endWiggle:) userInfo:touchedLayer repeats:NO];
    
    }
    
    -(void)endWiggle:(NSTimer*)timer {
    	// stopping the wiggle now
    	[((CALayer*)timer.userInfo) removeAllAnimations];
    }
    

    Now that we have two functions. We need add the touchesEnded method that gets called when you lift your finger off the screen.

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    	UITouch *touch = [[event allTouches] anyObject];
    
    	if ([touch view] == image) {
    		[self doWiggle:[touch view]];
    
    	}
    	else if ([touch view] == image2) {
    		[self doWiggle:[touch view]];
    	}
    }
    

    Now your image will wiggle when you end your drag.

    You can grab the source code here.

  • 相关阅读:
    C# PC版微信消息监听自动回复
    http ContentLength 为0 下载问题
    linq to entity不识别方法"System.String ToString()"
    android 环境搭建
    已备份数据库的磁盘上结构版本为 661。服务器支持版本 539,无法还原或升级此数据库
    android 百度地图 团队开发及正式apk发布
    Jquery UI Autocomplete 在mvc中应用
    <input type="image">表单提交2次 重复插入数据问题
    仿腾讯新闻时间轴效果
    项目管理笔记
  • 原文地址:https://www.cnblogs.com/ibadcool/p/1958668.html
Copyright © 2011-2022 走看看