解决方案 »
- cocos2d-x操作xml后进行保存,结果xml中没有变化
- iPhone字符串拼接问题
- 为什么模拟器上可以正常收发UDP广播,但是真机上收不了广播
- 关于iphone搜索PDF阿拉伯语的问题
- 我想学windows phone 开发,推荐几本基础教程
- xcode 编译出错
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 设置无效
- Xcode4.6.3中的iphone simulator6.1太大怎么改
- 【微信登陆】的乱码问题!
- 一个数组长度的问题
- 求理解 UIApplicationmain函数调用语法
- 如何隐藏TabelViewCell的边框线?
1. http://stackoverflow.com/questions/1555690/key-value-observing-during-uiview-animations
2. http://stackoverflow.com/questions/1557739/observing-animated-property-changes-in-a-calayer
3.https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/AnimatableProperties/AnimatableProperties.html#//apple_ref/doc/uid/TP40004514-CH11-SW1通过timer去选择性的获取属性变化是可靠的办法
动画可以做的属性见3,没有center, kvo观察它没有意义通过实验实现动画layer的delegate
观察postion靠谱,我自己实验了一个小程序,仅供参考,希望能对你有帮助~//
// ViewController.m
// ObsverveAnimationProperty
//
// Created by xiaomanwang on 14-3-7.
// Copyright (c) 2014年 xiaomanwang. All rights reserved.
//#import "ViewController.h"static const int duration = 2;@interface ViewController ()@property(nonatomic, strong)UIView*viewForAnimated;
@property(nonatomic, strong)NSTimer *timer;@end@implementation ViewController- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_viewForAnimated = [UIView new];
_viewForAnimated.backgroundColor = [UIColor redColor];
_viewForAnimated.frame = CGRectMake(20, 20, 100,100);
[self.view addSubview:_viewForAnimated]; _viewForAnimated.layer.delegate = self;
UIButton*btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"do" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:btn];
btn.frame = CGRectMake(200, 100, 40, 30);
[btn addTarget:self action:@selector(doAnimation) forControlEvents:UIControlEventTouchUpInside];
// [self addObserver:self forKeyPath:@"viewForAnimated.frame" options:NSKeyValueObservingOptionNew context:NULL];
[self addObserver:self forKeyPath:@"viewForAnimated.layer.presentationLayer.position" options:NSKeyValueObservingOptionNew context:NULL];
[self addObserver:self forKeyPath:@"viewForAnimated.layer.presentationLayer.bounds" options:NSKeyValueObservingOptionNew context:NULL];}- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//这里观察的presentationlayer没有东西,观察不到,动画的时候的presentationlayer是临时创建的
NSLog(@"change:%@",change);
}- (void)doAnimation
{
self.timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(getPos) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
CGRect frame = _viewForAnimated.frame;
if(_viewForAnimated.frame.origin.y >= 250.0)
{
frame.origin.y = 20;
}
else
{
frame.origin.y = 250;
}
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
_viewForAnimated.frame = frame;
} completion:^(BOOL finished) {
[self performSelector:@selector(stopTimer) withObject:nil afterDelay:duration];
}];
}- (void)stopTimer
{
if(self.timer)
{
[self.timer invalidate];
self.timer = nil;
}
}- (void)getPos
{
NSLog(@"current pos%@",NSStringFromCGPoint([[_viewForAnimated.layer presentationLayer] position]));
}- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event
{
NSLog(@"view.layer:%@",_viewForAnimated.layer);
NSLog(@"view.layer.presentationLayer:%@",_viewForAnimated.layer.presentationLayer);
//这里观察到的event是position
NSLog(@"layer:%@:event:%@",layer, event);
return nil;
}@end