【问题标题】:how can i change orientation by button's click events?如何通过按钮的单击事件更改方向?
【发布时间】:2010-01-23 20:22:13
【问题描述】:

我使用了 TableView 并且还通过单击披露按钮使用了披露按钮,我得到了一个特定的图像。我希望当我按下披露按钮时,我必须通过执行方向来获得那个特定的图像,所以我该怎么做。请帮我。我是这种应用程序的新手。

【问题讨论】:

    标签: iphone


    【解决方案1】:

    您需要手动转换表格。下面是一些创建 UITableView 并围绕其中心旋转的代码。

    // this defines a constant PORTRAIT_FRAME, which is the frame of the iPhone screen 
    // minus the Status Bar. Equivalent to CGRectMake(0,20,320,460). 
    #define PORTRAIT_FRAME [[UIScreen mainScreen] applicationFrame]
    
    // this defines a constant LANDSCAPE_FRAME, for orienting the table sideways
    #define LANDSCAPE_FRAME CGRectMake(0,20,480,300)
    
    
    // this function assumes you have a class variable called myTableView
    // and it toggles the orientation of myTableView
    - (void) rotateTable {
    
      if (myTableView.transform == CGAffineTransformMakeRotation(0)) {
        // rotate the image by 90 degrees  
        myTableView.transform = CGAffineTransformMakeRotation(M_PI/2); 
        myTableView.frame = LANDSCAPE_FRAME;
        return;
      }
    
      // set the image to its original orientation 
      myTableView.transform = CGAffineTransformMakeRotation(0); 
      myTableView.frame = PORTRAIT_FRAME;
    
    }
    

    如果您想在单击按钮时触发此旋转,则声明一个 UIButton 并将此函数指定为操作。这是一个创建 UIButton 的函数。只需将 @"rotateImage" 传递给动作,将 self 传递给目标。

    + (UIButton*) getButtonAtPoint:(CGPoint)point 
                                 target:(id)target 
                                 action:(NSString*)action {
    
      UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
      [button addTarget:target 
                 action:NSSelectorFromString(action)
       forControlEvents:UIControlEventTouchUpInside];
      return button;
    }
    

    【讨论】:

    • 非常感谢您的帮助,但实际上我使用的是表格视图,我的按钮不是普通按钮,但它是一个披露按钮,所以我该如何使用此代码?
    • 您可以设置 TableView 的转换,其方式与设置 imageView 的转换完全相同...即 myTable.transform = foo。此外,Disclosure 按钮只是一种 UIButton。我将编辑上面的代码以使用该类型的按钮。
    【解决方案2】:

    我认为您可能需要使用 UIApplication 的 setStatusBarOrientation:animated: 并手动旋转/调整顶级视图的大小。如果您使用 UIView 的动画块方法,那么您应该能够在动画块内执行此操作并提交它以获得基本相同的效果。不过我自己没试过。

    【讨论】:

    • 你能给我举个例子吗,因为我必须在按钮点击事件上这样做,我对此一无所知。
    猜你喜欢
    • 1970-01-01
    • 2020-12-30
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    相关资源
    最近更新 更多