iOS:滑动UIView on / off屏幕

前端之家收集整理的这篇文章主要介绍了iOS:滑动UIView on / off屏幕前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个应用程序上,左侧的“抽屉”将是非常有益的.我正在做一些初步的测试,看看我将如何最好地完成这个,我有一些非常基本的麻烦.

我的安装
我在Xcode 4中使用单视图应用程序模板.
2.对于xib的“main / border”视图,我添加了2个UIViews(LeftPanel和RightPanel)和一个UIButton(ShowHideButton).
我已经将LeftPanel的绿色和RightPanel蓝色化,以便更容易看清.
4.加载视图时,两个面板均可见,UIButton具有“隐藏面板”文本.
5.按下按钮后,LeftPanel应该滑出屏幕(向左),RightPanel应该展开以占用它的原始空间加上LeftPanel腾出的空间.
此时,ShowHideButton应将其文本更改为“显示面板”.
7.再次按下按钮时,LeftPanel应该滑回到屏幕上(从左边),RightPanel应该缩小到“放回”原来的空间.
此时,ShowHideButton应将其文本更改为“隐藏面板”.

我正在使用animateWithDuration实现动画:动画:完成:.到目前为止,关闭屏幕的转换工作正常(实际上很好).
令我困扰的是当我尝试把LeftPanel带回来的时候,我得到一个EXC_BAD_ACCESS.我已经发布了我的代码,我已经看了它,但我真的看不到我正在访问的那个被释放(或者导致EXC_BAD_ACCESS的任何事情).

DrawerTestingViewController.h
#import <UIKit/UIKit.h>

typedef enum {
    kHidden,kShown
} PanelState;

@interface DrawerTestingViewController : UIViewController {

    PanelState   currentState;

    UIButton    *showHideButton;

    UIView      *leftPanel;
    UIView      *rightPanel;
}

@property (assign,nonatomic)          PanelState   CurrentState;

@property (strong,nonatomic) IBOutlet UIButton     *ShowHideButton;

@property (strong,nonatomic) IBOutlet UIView       *LeftPanel;
@property (strong,nonatomic) IBOutlet UIView       *RightPanel;

- (IBAction)showHidePressed:(id)sender;

@end
DrawerTestingViewController.m
#import "DrawerTestingViewController.h"

@implementation DrawerTestingViewController

@synthesize CurrentState    = currentState;
@synthesize LeftPanel       = leftPanel;
@synthesize RightPanel      = rightPanel;
@synthesize ShowHideButton  = showHideButton;

#pragma mark - My Methods

- (IBAction)showHidePressed:(id)sender
{
    switch ([self CurrentState]) {
        case kShown:
            // Hide the panel and change the button's text
            // 1. Hide the panel
            [UIView animateWithDuration:0.5 
                animations:^{
                // b. Move left panel from (0,w,h) to (-w,h)
                CGRect currLeftPanelRect = [[self LeftPanel] frame];
                currLeftPanelRect.origin.x = -1 * currLeftPanelRect.size.width;
                [[self LeftPanel] setFrame:currLeftPanelRect];
                // c. Expand right panel from (x,h) to (0,w + x,h)
                CGRect currRightPanelRect = [[self RightPanel] frame];
                currRightPanelRect.origin.x = 0;
                currRightPanelRect.size.width += currLeftPanelRect.size.width;
                [[self RightPanel] setFrame:currRightPanelRect];}
                completion:NULL];
            // 2. Change the button's text
            [[self ShowHideButton] setTitle:@"Show Panel" forState:UIControlStateNormal];
            // 3. Flip [self CurrentState]
            [self setCurrentState:kHidden];
            break;
        case kHidden:
            // Show the panel and change the button's text
            // 1. Show the panel
            [UIView animateWithDuration:0.5 
                animations:^{
                // b. Move left panel from (-w,h)
                CGRect currLeftPanelRect = [[self LeftPanel] frame];
                currLeftPanelRect.origin.x = 0;
                [[self LeftPanel] setFrame:currLeftPanelRect];
                // c. Expand right panel from (0,h) to (leftWidth,w - leftWidth,h)
                CGRect currRightPanelRect = [[self RightPanel] frame];
                currRightPanelRect.origin.x = currLeftPanelRect.size.width;
                currRightPanelRect.size.width -= currLeftPanelRect.size.width;
                [[self RightPanel] setFrame:currRightPanelRect];}
                completion:NULL];
            // 2. Change the button's text
            [[self ShowHideButton] setTitle:@"Hide Panel" forState:UIControlStateNormal];
            // 3. Flip [self CurrentState]
            [self setCurrentState:kShown];
            break;
        default:
            break;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setCurrentState:kShown];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    switch ([self CurrentState]) {
        case kShown:
            [[self ShowHideButton] setTitle:@"Hide Panel" forState:UIControlStateNormal];
            break;
        case kHidden:
            [[self ShowHideButton] setTitle:@"Show Panel" forState:UIControlStateNormal];
            break;
        default:
            break;
    }
}

@end

我错过了一些超基本的东西吗?有人可以帮忙吗?

谢谢!

编辑:
我再试过两件事:
问题似乎与将屏幕外观看在屏幕上有关,因为LeftPanel离屏开始让我有同样的问题.
2.通过代码可靠地导致Xcode(4 Beta for Lion)崩溃.以下是详细信息(每次崩溃都是这样):

/SourceCache/DVTFoundation/DVTFoundation-867/Framework/Classes/FilePaths/DVTFilePath.m:373中的ASSERTION FAILURE
详细信息:空字符串不是有效的路径
目的:
方法:_filePathForParent:fileSystemRepresentation:length:allowCreation:
线程:{name =(null),num = 55}
提示:无
回溯:
0 0x00000001068719a6 – [IDEAssertionHandler handleFailureInMethod:object:fileName:lineNumber:messageFormat:arguments:](在IDEKit中)
1 0x0000000105f3e324 _DVTAssertionFailureHandler(在DVTFoundation中)
2 0x0000000105edd16f [DVTFilePath _filePathForParent:fileSystemRepresentation:length:allowCreation:](在DVTFoundation中)
3 0x0000000105edcd4d [DVTFilePath _filePathForParent:pathString:](在DVTFoundation中)
4 0x0000000105ede141 [DVTFilePath filePathForPathString:](在DVTFoundation中)
5 0x00000001064a8dde – [IDEIndex queryProviderForFile:highPriority:](在IDEFoundation中)
6 0x000000010655193b – [IDEIndex(IDEIndexQueries)symbolsMatchingName:inContext:withCurrentFileContentDictionary:](在IDEFoundation中)
7 0x000000010aca6166 __68- [IDESourceCodeEditor symbolsForExpression:inQueue:completionBlock:] _ block_invoke_01561(在IDESourceEditor中)
8 0x00007fff93fb490a _dispatch_call_block_and_release(在libdispatch.dylib中)
9 0x00007fff93fb615a _dispatch_queue_drain(在libdispatch.dylib中)
10 0x00007fff93fb5fb6 _dispatch_queue_invoke(在libdispatch.dylib中)
11 0x00007fff93fb57b0 _dispatch_worker_thread2(在libdispatch.dylib中)
12 0x00007fff8bb5e3da _pthread_wqthread(在libsystem_c.dylib中)
13 0x00007fff8bb5fb85 start_wqthread(在libsystem_c.dylib中)

更新:屏幕截图
显示面板(启动状态)

面板隐藏(按钮按下后成功转换)

错误:再次按下按钮会导致故障

解决方法

在玩了一堆,撞在墙上,我终于得出结论,我的代码没有错,除非我误解了一些块.在调试过程中,我的代码完全像我预期的一样,但是在动画块结束和完成块的开始之间,EXC_BAD_ACCESS不断弹出.

无论如何,我不知道我在哪里得到这个想法,但我想我可能想尝试在我的动画块之外做我的数学计算(改变框架).

你猜怎么了?有效!

所以,毫不费力,这里是我想要的工作代码

- (IBAction)showHidePressed:(id)sender
{
    switch ([self CurrentState]) {
        case kShown:
        {
            // Hide the panel and change the button's text
            CGRect currLeftPanelRect = [[self LeftPanel] frame];
            currLeftPanelRect.origin.x -= currLeftPanelRect.size.width / 2;
            CGRect currRightPanelRect = [[self RightPanel] frame];
            currRightPanelRect.origin.x = 0;
            currRightPanelRect.size.width += currLeftPanelRect.size.width;
            // 1. Hide the panel
            [UIView animateWithDuration:0.5 
              animations:^{
              // b. Move left panel from (0,h)
              [[self LeftPanel] setFrame:currLeftPanelRect];
              // c. Expand right panel from (x,h)
              [[self RightPanel] setFrame:currRightPanelRect];
              }
              completion:^(BOOL finished){ if(finished) {
              [[self ShowHideButton] setTitle:@"Show Panel" forState:UIControlStateNormal];
              [self setCurrentState:kHidden];
              }
              }];
        }            
            break;
        case kHidden:
        {
            // Show the panel and change the button's text
            // 1. Show the panel
            [UIView animateWithDuration:0.5 
              animations:^{
              // b. Move left panel from (-w,h)
              CGRect currLeftPanelRect = [[self LeftPanel] frame];
              currLeftPanelRect.origin.x += currLeftPanelRect.size.width / 2;
              [[self LeftPanel] setFrame:currLeftPanelRect];
              // c. Expand right panel from (0,h)
              CGRect currRightPanelRect = [[self RightPanel] frame];
              currRightPanelRect.origin.x = currLeftPanelRect.size.width;
              currRightPanelRect.size.width -= currLeftPanelRect.size.width;
              [[self RightPanel] setFrame:currRightPanelRect];
              }
              completion:^(BOOL finished){ if(finished) {
              [[self ShowHideButton] setTitle:@"Hide Panel" forState:UIControlStateNormal];
              [self setCurrentState:kShown];
              }
              }];
        }
            break;
        default:
            break;
    }
}
原文链接:https://www.f2er.com/iOS/330422.html

猜你在找的iOS相关文章