[英]Replicate block animation ios [UIView animateWithDuration:…]
I would like to replicate something like the built-in UIView
class method +animateWithDuration:animations:completion:
. 我想复制类似于内置
UIView
类方法+animateWithDuration:animations:completion:
。 So basically I am trying to write a method with a block where I can access/manipulate the inner variables. 因此,基本上我想尝试编写一个带有块的方法,在其中我可以访问/操作内部变量。
Something like this: 像这样:
int dur = 0.5;
[MyClass changeWithDuration:dur manipulations:^{
// I want to access these values and change them to the following values
// in 'dur' seconds, just like the built-in UIView class function does
myVariable = 0.5;
myOtherVariable = 1;
}];
I would like to use this in a Cocoa
application, so using the UIView
class method is out of the question. 我想在
Cocoa
应用程序中使用它,因此使用UIView
类方法是UIView
的。
If you want to access and modify variable you should add __block keyword: 如果要访问和修改变量,则应添加__block关键字:
__block int dur = 0.5;
[MyClass changeWithDuration:dur manipulations:^{
// I want to access these values and change them to the following values
// in 'dur' seconds, just like the built-in UIView class function does
myVariable = 0.5;
myOtherVariable = 1;
//Now you can change value go due variable
dur = 2.5;
}];
If there is a object you want to access, sometimes you need to create weak reference to that object to avoid retain cycles. 如果存在要访问的对象,有时需要创建对该对象的弱引用以避免保留周期。
//Extended //扩展
If you want to do something similar to UIView animation blocks declare method in .h file: 如果要执行类似于UIView动画块的操作,请在.h文件中声明方法:
// This just save typings, you don't have to type every time 'void (^)()', now you have to just type MyBlock
typedef void (^MyBlock)();
@interface MyClass : UIView
-(void)changeWithDuration:(float)dur manipulations:(MyBlock)block;
@end
And in your .m file: 在您的.m文件中:
-(void)changeWithDuration:(float)dur manipulations:(MyBlock)block
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:dur];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if (block)
block();
[UIView commitAnimations];
}
To call the method you can do: 要调用该方法,您可以执行以下操作:
vi = [[MyView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[vi setBackgroundColor:[UIColor redColor]];
[self.view addSubview:vi];
[vi changeWithDuration:3 manipulations:^{
vi.frame = CGRectMake(250, 20, 50, 40);
}];
Is it something you are after? 你在追求什么吗?
Okay, it does not seem to be possible... 好的,似乎不可能...
iOS - Getting a variable from inside a block passed as a parameter
iOS-从作为参数传递的块中获取变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.