带有按钮的 Uiscroll视图中的动画



我已经尝试了iCarousel,并将其添加到我的项目中。我能够放置图像。现在我正在尝试实现一种方法,当单击按钮时,轮播将旋转。我试过这段代码:

- (IBAction) spinButton:(id)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:carousel
                             cache:YES];
    [spinButton removeFromSuperview];
    [UIView commitAnimations];
}

但似乎只有轮播视图是动画。

以下两种方法可以改变白羊座可能是您的问题,由此解决,

-(void)TappedButton:(UIButton *)sender{
    UIView *view=[carousel itemViewAtIndex:sender.tag];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:YES];
    //    [spinButton removeFromSuperview];
    [UIView commitAnimations];
    NSLog(@"%d",sender.tag);
}             
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;
    //create new view if no view is available for recycling
    if (view == nil)
    {
        view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page.png"]] autorelease];
//      label = [[[UILabel alloc] initWithFrame:view.bounds] autorelease];
//      label.backgroundColor = [UIColor clearColor];
//      label.textAlignment = UITextAlignmentCenter;
//      label.font = [label.font fontWithSize:50];
//        
//        [view addSubview:label];
        UIButton *buttonForDisplay = [[[UIButton alloc] initWithFrame: view.bounds] autorelease];
        buttonForDisplay.backgroundColor=[UIColor clearColor];
        [buttonForDisplay setTitle:[NSString stringWithFormat:@"%d",index] forState:UIControlStateNormal];
        buttonForDisplay.titleLabel.font = [label.font fontWithSize:50];
        [buttonForDisplay addTarget:self action:@selector(TappedButton:) forControlEvents:UIControlEventTouchUpInside];
        [buttonForDisplay bringSubviewToFront:view];
        buttonForDisplay.tag = index;
///        view.tag=index;
        [view addSubview:buttonForDisplay];

    }
    else
    {
        label = [[view subviews] lastObject];
    }
    //set label
//  label.text = [[items objectAtIndex:index] stringValue];
    return view;
}

编辑此方法通过您可以捕获轮播视图的引用。

- (UIView *)itemViewAtIndex:(NSInteger)index

使用动画编辑

UIViewController *nextViewController = [[UIViewController alloc] autorelease];
nextViewController.view.backgroundColor = [UIColor redColor];  
CATransition *animation = [CATransition animation];
animation.duration = 3.5;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromTop;  
[self.navigationController pushViewController:nextViewController animated:NO];
[self.navigationController.view.layer addAnimation:animation forKey:nil];

最新更新