0

我有一种方法可以下拉一个UIView. 这很好用。当第二次触摸按钮时,我将如何让它向上滑动?

我还需要以某种方式停用它,以便它不会重复动画,如果它已经显示。

- (void)slideDownTableView {

    self.tableViewCities = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    self.tableViewCities.frame = CGRectMake(0,0,300,180);
    self.tableViewCities.autoresizingMask = ( UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth );
    self.tableViewCities.dataSource = self;
    self.tableViewCities.delegate = self;
    self.tableViewCities.backgroundColor = [UIColor whiteColor];


    UIView *myview=[[UIView alloc] initWithFrame: CGRectMake(10, 0,300,180)];
    myview.backgroundColor=[UIColor redColor];

    [myview addSubview:self.tableViewCities];

    [self.view addSubview:myview];
    myview.frame = CGRectMake(10, 0,300,-180); // offscreen

        [UIView animateWithDuration:0.5
                         animations:^{
                             myview.frame = CGRectMake(10, 0,300,180); // final location move
                         }];
}
4

5 回答 5

2

iOS 提供了 autoreverse 属性,通过该属性动画会自动反转。

UIViewAnimationOptionAutoreverse当动画反转时会给你一个效果。

   [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationOptionAutoreverse   
                     animations:^{
                         // do whatever animation you want, e.g.,

                         someView.frame = someFrame1;
                     }
                     completion:NULL];
于 2013-09-21T10:24:32.917 回答
1

使用BeginFromCurrentState选项对我有用。

于 2016-07-25T12:01:32.657 回答
0

使用一个简单的 BOOL 来存储滑动 UIView 的当前状态,您可以实现这一点。

如果我理解得很好,初始状态是不可见的?所以最初 BOOL cityVisible 设置为 N0,然后按钮的动作检查它的值,并相应地执行不同的动画。简单的。

最后,我还在动画期间禁用了按钮,以防止用户在第一个动画已经执行时触发第二个动画。

我重写了一些代码以遵循一些最佳实践,并提高可读性/可维护性:

  • 预先设置视图,因为按钮的操作职责不应该是设置视图,而只是为它们设置动画。
  • 更改了目标操作的签名,这是我个人的风格指南,用于说明发件人的姓名和 ControlEvent,当您有多个按钮/操作时,它会更加整洁;)。同样根据 Apple 指南,接收操作的方法应该有一个“id”参数,即发送者。
  • 将帧存储在变量中(您也可以将它们设置为#define,我认为这会更好),因此您不必对它们进行两次硬编码,它通过查看视图来降低破坏动画的风险滑到不同的位置...

代码更新:

@interface ViewController ()

@property (nonatomic, strong) UIButton *showCitiesButton;

@property (nonatomic, strong) CGRect citiesInitialFrame;
@property (nonatomic, strong) CGRect citiesVisibleFrame;

@property (nonatomic, strong) UITableView *tableViewCities;
@property (nonatomic, strong) UIView *citiesContainer;

@property (nonatomic, strong) BOOL citiesVisible;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Setting up the cities view
    self.citiesInitialFrame = CGRectMake(10, -180, 300, 180);
    self.citiesVisibleFrame = CGRectMake(10, 0, 300, 180);

    self.tableViewCities = [[UITableView alloc] init ...];
    self.citiesContainer = [[UIView alloc] init ...];

    // ...

    self.citiesVisible = NO;

    [self.showCitiesButton addTarget:self
                              action:@selector(showCitiesButtonDidTouchUpInside:)
                    forControlEvents:UIControlEventTouchUpInside];
}

- (void)showCitiesButtonDidTouchUpInside:(id)sender
{
    [self.showCitiesButton setEnabled:NO];
    if (self.citiesVisible)
    {
        // Cities view is visible, sliding it out of the screen
        [UIView animateWithDuration:0.5
                         animations:^{
                             self.citiesContainer.frame = self.citiesInitialFrame;
                         } completion:^(BOOL finished) {
                             self.citiesVisible = NO;
                             [self.showCitiesButton setEnabled:YES];
                         }];
    }
    else
    {
        // Cities view is not visible, sliding it in
        [UIView animateWithDuration:0.5
                         animations:^{
                             self.citiesContainer.frame = self.citiesVisibleFrame;
                         } completion:^(BOOL finished) {
                             self.citiesVisible = YES;
                             [self.showCitiesButton setEnabled:YES];
                         }];
    }

}

@end
于 2013-09-21T10:50:49.707 回答
0
if(!enabled){
            [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationTransitionCurlUp
                             animations:^{
              myview.frame = CGRectMake(10, 0,300,180);

     }
      completion:nil];
       enabled=YES;

}
else{
     [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationTransitionCurlUp
                             animations:^{
              myview.frame = CGRectMake(10, 0,300,-180);

     }
      completion:nil];
       enabled=NO;
}
于 2013-09-21T10:21:32.113 回答
0

您可以使用以下代码反转 UIView 动画:

if (isMarked) {

    [UIView animateWithDuration:3
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:(void (^)(void)) ^{
                         self.transform=CGAffineTransformMakeScale(1.1, 1.1);
                     }
                     completion:nil];

} else {

    [UIView animateWithDuration:3
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:(void (^)(void)) ^{
                         self.transform=CGAffineTransformIdentity;
                     }
                     completion:nil];
}
于 2016-02-02T14:55:48.480 回答