ios - 使用 UIViewAnimationTransitionFlipFromRight 在按下时翻转 UIButton 并在 "the other side"上显示新按钮

标签 ios uiview uibutton uiviewanimation uiviewanimationtransition

为 MOXY 编辑。

我有一组按钮,顶部有一个图像,还有一组按钮的 View 是背景色。当我单击一个按钮时,我想检索该按钮索引并使用该索引来获取另一个 View 的适当背景颜色..并使用该信息翻转按钮(带有图像)并将其“翻转”到显示背景颜色。

我的屏幕的组成是 ViewControllersView->TileView->(按钮数组)。

 -(void)viewDidAppear:(BOOL)animated{
 _viewContents = [model getRequestedView];  //this retrieves an array of UIButtons(24 
  //of them) whose frames and background colors have already been initialized to fit
   //within the TileView.
 _viewFrontImageContents =[model getRequestedViewFrontCardImagesWithFrame:_viewContents];

for(int screenViewIndex = 0; screenViewIndex < [_viewContents count]; screenViewIndex++){
    [TileView addSubview:[_viewFrontImageContents objectAtIndex:screenViewIndex]];
    [[_viewFrontImageContents objectAtIndex:screenViewIndex] addTarget:self action:@selector(frontOfCardPushed:) forControlEvents:UIControlEventTouchUpInside];
    //[TileView addSubview:[_viewContents objectAtIndex:screenViewIndex]];
    //[[_viewContents objectAtIndex:screenViewIndex] addTarget:self action:@selector(frontOfCardPushed:) forControlEvents:UIControlEventTouchUpInside];
}

}

//getRequestedViewFrontCardImagesWithFrame方法

-(NSMutableArray *)getRequestedViewFrontCardImagesWithFrame:(NSArray *)views{
        NSMutableArray *cards = [[NSMutableArray alloc] init];
        UIImage *frontOfCardImage = [UIImage imageNamed:@"Back_of_Tile"];
        UIButton *frontOfCardView;

        for(int viewIndex = 0; viewIndex < [views count]; viewIndex++){
            frontOfCardView = [[UIButton alloc] initWithFrame:[[views objectAtIndex:viewIndex] frame]];
            frontOfCardView.backgroundColor = [UIColor colorWithPatternImage:frontOfCardImage];
  //setting the tag of each frontCard to match the view index to keep them aligned.
            frontOfCardView.tag = viewIndex;
            [cards addObject:frontOfCardView];

            if(viewIndex == 0){
              NSLog(@"first frontcard frame %@, first _viewContentsFrame %@", [cards objectAtIndex:0], [views objectAtIndex:0]);
    }
        }
        return cards;
    }

上面我添加了 UIButtons,顶部有一个图像来表示“扑克牌的正面”。所述按钮的索引与 _viewContents 数组的索引完全匹配。我设置每个按钮以匹配已经初始化的按钮的框架(大小和位置)。

-(IBAction)frontOfCardPushed:(id)sender{
int containerViewIndex = -1;
UIButton *pressedButton = (UIButton *)sender;
NSLog(@"Button Index: %i", pressedButton.tag);
containerViewIndex = pressedButton.tag;
UIView *container = [TileView viewWithTag:containerViewIndex];
UIView *viewToShow = [_viewContents objectAtIndex:containerViewIndex];
NSLog(@"container: %@, viewToShow: %@", container, viewToShow);
//viewToShow.frame = container.frame;
//NSLog(@"container: %@, viewToShow: %@", container, viewToShow);

[UIView transitionWithView:container duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight
                animations:^{
                    [sender removeFromSuperview];
                    [container addSubview:viewToShow];
                }
                completion:nil];

我想要做的是使用一个转换——即“UIViewAnimationTransitionFlipFromRight”,以便在按下时“翻转卡片”以显示关联的 _viewContents 数组的内容。我还希望用户能够将卡片翻转过来以显示 frontCardButton 的图像。上面的这段代码只关注卡片的第一次翻转,以避免代码墙。

我看到了合适的背景颜色 - 但动画本身不工作。

我做错了什么,我需要向这段代码添加/删除什么才能使其正常工作?

  • 编辑* 正如下面对 Moxy 的说明,请查看 getRequestedViewFrontCardImagesWithFrame 方法中的输出。查看第一张卡片的输出,然后查看 frontCardPushed: 方法中容器 View 的输出,在请求带有适当标签的 View 后 - 它们不匹配。我正在打印按钮索引,以便您可以看到我正在点击第一个 TileView subview 。 _viewContents View 的标签是一个标签集,代表一种颜色,与我在 getRequestedViewFrontCardImagesWithFrame 方法中分配的 TileView 标签无关。然而,令人费解的是,在我将标签分配给第一个按钮并打印出来后,标签并未列出。

输出:

//why doesnt the first frontcard frame list a tag? I had just assigned it one?

first frontcard frame UIButton: 0x1c53f020; frame = (6 5; 145 145); opaque = NO; layer = CALayer: 0x1c534ac0, first _viewContentsFrame UIButton: 0x1d045220; frame = (6 5; 145 145); opaque = NO; tag = 3; layer = CALayer: 0x1d04535

Button Index: 0
container: UIView: 0x1c53a400; frame = (51 83; 922 614); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = NSArray: 0x1d05afe0>; layer = CALayer: 0x1c53a460, viewToShow: UIButton: 0x1d045220; frame = (6 5; 145 145); opaque = NO; tag = 3; layer = CALayer: 0x1d045350

最佳答案

一个更简单的方法是使用 [UIView transitionWithView]:

[UIView transitionWithView:self.flipButton
                  duration:0.8
                   options:displayingFirstButton ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight
                animations:^{
                  //change image for button
                }
                completion:^(BOOL finished) {

                }];

关于ios - 使用 UIViewAnimationTransitionFlipFromRight 在按下时翻转 UIButton 并在 "the other side"上显示新按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15212238/

相关文章:

ios - 在 UITableViewCell 的附件 View 中使用自定义按钮的问题

ios - iOS 7 中的 UITextView 行间距已更改 - 如何使用 Core Text 计算旧间距

ios - 使用Facebook登录名时,如何访问quickblox session token ?

ios - 当年份小于 2000 时,initWithTimeIntervalSinceReferenceDate 返回 null

ios - 从弹出窗口关闭 UIview IOS 7

objective-c - UITableViewCell subview 的 NSIndexPath?

iphone - 如何检查 NSString = 特定字符串值?

ios - NSLayoutConstraints 出现问题并以编程方式使用自动布局时,UIViews 停留在 (0,0)

ios - 为什么我的 UIImage 占用这么多内存?

iphone - UIButton 文本颜色 RGB