我想在 iOS 中创建自定义动画。假设,我有一个 UIView 显示它的子视图。我想截取它的屏幕截图并将它分成四个部分,应该是左上、右上、左下、右下从中间并将每个部分移动到它们的角落。因为左上应该向左移动UIView 的上角和右上角应该移动到右上角等等。
user2125861
问问题
1037 次
2 回答
0
可能这个对你有帮助但不确定,首先设置你的框架位置,然后改变你想要的框架位置,默认情况下我正在采取
top.frame=CGRectMake(0,0,320,120);
bottom.frame=CGRectMake(0,315,320,145);
left.frame=CGRectMake(0,113,151,202);
right.frame=CGRectMake(175,113,151,202);
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(tranform) userInfo:nil repeats:NO];
3秒后此方法将调用,
-(void)tranform{
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationTransitionCurlUp
animations:^{
top.frame=CGRectMake(0,-120,320,120);
bottom.frame=CGRectMake(0,480,320,145);
left.frame=CGRectMake(-151,113,151,202);
right.frame=CGRectMake(320,113,151,202);
}
completion:nil];
}
在上面的代码中,顶部、底部、左侧、右侧是四个视图,我将所有这些添加到 self.view 中,当单击特定时,动画会发生在它们各自的帧上。
于 2013-03-02T07:38:47.630 回答
0
这是我的答案,它将截取当前屏幕显示的屏幕截图,将其从中心分成四个相等的部分,并将其动画移动到各自的角落。
- (IBAction)animate:(id)sender
{
NSLog(@"animation called ");
UIGraphicsBeginImageContext(self.view.window.bounds.size);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imgref=[image CGImage];
NSArray *arrayOfImages= [[NSArray alloc]initWithArray:[self splitImageIntoRects:imgref]];
NSLog(@"array of images are %@",arrayOfImages);
for (CALayer *layer in arrayOfImages) {
[self.view.layer addSublayer:layer];
}
[self animateLayers:arrayOfImages];
}
-(void)animateLayers:(NSArray*)array
{
for (int i=0;i<array.count;i++)
{
CALayer *layer=[array objectAtIndex:i];
CGRect rect=[layer frame];
CGPoint newPt;
switch (i) {
case 0:
newPt=CGPointMake(-rect.size.width, -rect.size.height);
break;
case 1:
newPt=CGPointMake(rect.origin.x-rect.size.width, rect.origin.y+rect.size.height);
break;
case 2:
newPt=CGPointMake(rect.origin.x+rect.size.width, rect.origin.y-rect.size.height);
break;
case 3:
newPt=CGPointMake(rect.origin.x+rect.size.width, rect.origin.y+rect.size.height);
break;
}
[self moveLayer:layer to:newPt];
}
}
-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration=1;
animation.fromValue = [layer valueForKey:@"position"];
animation.toValue = [NSValue valueWithCGPoint:point];
layer.position = point;
[layer addAnimation:animation forKey:@"position"];
}
- (NSArray*)splitImageIntoRects:(CGImageRef)anImage{
CGSize imageSize = CGSizeMake(CGImageGetWidth(anImage), CGImageGetHeight(anImage));
NSMutableArray *splitLayers = [NSMutableArray array];
int kXSlices = 2;
int kYSlices = 2;
for(int x = 0;x < kXSlices;x++) {
for(int y = 0;y < kYSlices;y++) {
CGRect frame = CGRectMake((imageSize.width / kXSlices) * x,
(imageSize.height / kYSlices) * y,
(imageSize.width / kXSlices),
(imageSize.height / kYSlices));
CALayer *layer = [CALayer layer];
layer.frame = frame;
CGImageRef subimage = CGImageCreateWithImageInRect(anImage, frame);
layer.contents = (__bridge id)subimage;
CFRelease(subimage);
[splitLayers addObject:layer];
}
}
return splitLayers;
}
于 2013-03-04T04:35:38.387 回答