本文介绍了通过子视图中的IBAction禁用滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPad应用程序目前是这样构建的:xml数据从我的网站中拉出,并在View ControllerFishIDView中解析。这个视图控制器有一个UIScrollViewscrollView,它有一个视图XMLView,它是一个UIView。 scrollView被设置为使得它基于XML中包含的项目的数量来滚动和浏览XMLViews。例如12个项目= 12页或XMLView实例。这些页面包含单个鱼的照片。这些页面中的每一个都包含一个按钮,用于创建名为flipView的UIViewXMLView的子视图,并通过左侧动画转换的翻转显示子视图。此子视图包含有关鱼的信息。 flipView有一个按钮,返回用户(翻转回)到鱼图片。

My iPad app is currently built as such: xml data is pulled from my website and parsed in a View Controller "FishIDView". This view controller has a UIScrollView "scrollView" that has a subview "XMLView" which is a UIView. scrollView is setup so that it scrolls and pages through the XMLViews based on the number of items contained in the XML. For example 12 items = 12 pages, or instances of XMLView. These pages contain photos of individual fish. Each of these pages contain a button that creates a subview of the UIView "XMLView" called "flipView", and displays the subview via the flip from left animated transition. This subview contains information about the pictured fish. flipView has a button that returns the user (flips back) to the fish picture.

我的问题是,当我查看flipView(它是一个视图,它是一个scrollView的子视图的XMLView的子视图),滚动仍然启用。如果我在flipView上向左或向右滚动,我会看到下一个鱼的XMLView。我想要滚动来查看flipView时被禁用。

My problem is that when I'm looking at the flipView (which is a subview of XMLView which is a subview of scrollView), scrolling is still enabled. If I scroll left or right while on flipView, I see the XMLView of the next fish. I want scrolling to be disabled while looking at flipView.

有什么建议可以从子视图中发送类似setScrollEnabled:NO命令到scrollView(在FishIDView上)?

Any suggestions on what I can do to send something like a setScrollEnabled:NO command to scrollView (on FishIDView) from the subview?

编辑:

所以我假设我需要使用协议和委托。我以为我可以弄清楚,但是我正在抓紧执行。

So I assume I need to use a protocol and delegate. I thought I could figure it out, but I'm getting caught up on the implementation.

在我的XMLView.h中(没有外部代码):

In my XMLView.h (leaving out extraneous code):

@protocol XMLViewDelegate <NSObject>

- (void) stopScrolling;

@end

@interface XMLView : UIView 
{
    ...
    id                      secondDelegate;
}
...
@property (nonatomic, retain) id <XMLViewDelegate>    secondDelegate;
...

@end

然后在我的XMLView .m(IBAction挂钩到一个可以正常工作的UIButton):

Then in my XMLView.m (with the IBAction hooked to a UIButton which works correctly):

...
@synthesize secondDelegate;
...
-(IBAction)goToInfo
{
    //[self newPage];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
    [self.secondDelegate stopScrolling];
    [self addSubview:flipView];
    ...

    NSLog(@"button pressed");

}

在FishIDView.h中:

In FishIDView.h:

@interface FishIDView : UIViewController <XMLViewDelegate>

FishIDView.m:

FishIDView.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [scrollView setScrollEnabled:YES];

    XMLView *subsubView = [[XMLView alloc] init];
    subsubView.secondDelegate = self;
    ...
}

-(void) stopScrolling
{
    [scrollView setScrollEnabled:NO];
    NSLog(@"NO more scrolling!!!");
}

当我点击XMLView中的按钮触发goToInfo时,其余的的动作发生,但日志不再滚动!!从来没有显示。

When I click on my button in XMLView that triggers "goToInfo", the rest of the action happens, but the log "No more scrolling!!" never displays.

我错了吗?我还在试图找出代表,所以我可能完全错了这种方法。

Did I go about this the wrong way? I'm still trying to figure out delegates, so I may be completely wrong with this methodology.

编辑2:

我现在已经试图摆脱委托人的行为,看起来像一个更简单的路线,但仍然无法正常工作。

I've now tried getting rid of the delegate and going what sounds like a more simplistic route, but it's still not working correctly.

在XMLView.m中:

In XMLView.m:

-(IBAction)goToInfo
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];

    //The next 2 lines are what I added
    ViewController *mainView = [[ViewController alloc] init];
    [mainView stopScrolling];

    [self addSubview:flipView];
    [infoButton setEnabled:FALSE];
    [infoButton setHidden:YES];
    [title setHidden:YES];
    [UIView commitAnimations];

    NSLog(@"button pressed");

}

然后在FishIDView.h

Then in FishIDView.h

-(void) stopScrolling;

FishIDView.m

FishIDView.m

-(void) stopScrolling
{
    [scrollView setScrollEnabled:NO];
    NSLog(@"NO more scrolling!!!");
}

不再滚动!!!打印,但scrollView仍然滚动启用!为什么会运行stopScrolling的NSLog部分,但不是setScrollEnabled:否?有人可以帮我吗

The "No more scrolling!!!" prints, but scrollView is still scroll enabled! Why would it run the NSLog portion of stopScrolling but not setScrollEnabled:NO? Can anyone help me?

推荐答案

已解决!我收录了通知,而不是直接从子视图中调用方法。

Solved! I incorporated Notifications instead of trying to directly call a method from a subview.

我的设置快速概述。 UIViewController FishIDView包含一个名为scrollView的UIScrollView。 scrollView具有子视图XMLView。 XMLView有一个按钮,显示其子视图,flipView。我希望scrollView在scrollView可见时被禁用。

Quick overview of my setup. UIViewController FishIDView contains a UIScrollView called scrollView. scrollView has a subview XMLView. XMLView has a button that shows its subview, flipView. I wanted scrolling to be disabled on scrollView when flipView was visible.

在XMLView.h中(省略不相关的代码):

In XMLView.h (with irrelevant code omitted):

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

#define kApplicationDidStopScrollingNotification @"StopScroll"
#define kApplicationDidResumeScrollingNotification @"ResumeScroll"

#import "FishIDView.h"

@interface XMLView : UIView 
{
    IBOutlet UIButton       *infoButton;
    IBOutlet UIButton       *closeButton;
    IBOutlet UIView         *flipView; 
}

@property (nonatomic, retain) IBOutlet UIButton       *infoButton;
@property (nonatomic, retain) IBOutlet UIButton       *closeButton;

- (IBAction)goToInfo;

- (IBAction)closeSubView;

@end

在XMLView.m中:

In XMLView.m:

#import "XMLView.h"

@implementation XMLView

@synthesize infoButton, closeButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

//Allows button to be pressed while contained in subview of scrollview
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint hitPoint = [infoButton convertPoint:point fromView:self];
    CGPoint closePoint = [closeButton convertPoint:point fromView:flipView];
    if ([infoButton pointInside:hitPoint withEvent:event]) return infoButton;
    if ([closeButton pointInside:closePoint withEvent:event]) return closeButton;
    return [super hitTest:point withEvent:event];  
}

-(IBAction)goToInfo
{
    //post notification for FishIDView to listen to to trigger method that disables scrolling
    [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidStopScrollingNotification object:nil];

    [self addSubview:flipView];

    //custom transition between views. A cross-fade effect
    flipView.alpha = 0.0f;
    [UIView beginAnimations:@"fadeInSecondView" context:NULL];
    [UIView setAnimationDuration:0.5];
    flipView.alpha = 1.0f;

    [infoButton setEnabled:FALSE];

    [UIView commitAnimations];   
}

- (IBAction)closeSubView
{
    //post notification to resume scrolling
    [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidResumeScrollingNotification object:nil];

    flipView.alpha = 1.0f;
    [UIView beginAnimations:@"fadeInSecondView" context:NULL];
    [UIView setAnimationDuration:0.5];
    flipView.alpha = 0.0f;

    [infoButton setEnabled:TRUE];

    [UIView commitAnimations];
    [flipView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.2];

}

@end

确保将#importXMLView.h放在顶部的FishIDView.h文件中,并在底部创建 - (void)方法。

Make sure to put #import "XMLView.h" into your FishIDView.h file at the top and create the -(void) methods at the bottom.

#import <UIKit/UIKit.h>

#import "XMLView.h"

@class AppDelegate;

@interface FishIDView : UIViewController <UIScrollViewDelegate>
{
...
}

@property ...

//These are the 2 methods that turn scrolling on and off
-(void) stopScrolling;
-(void) resumeScrolling;

@end

然后在FishIDView.m

Then in FishIDView.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [scrollView setScrollEnabled:YES];

    //Listen for the notification to stop scrolling
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopScrolling) name:kApplicationDidStopScrollingNotification object:nil];

    //Listen for the notification to resume scrolling
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeScrolling) name:kApplicationDidResumeScrollingNotification object:nil];
    [self layoutSubview];
}

-(void) stopScrolling
{    
    [scrollView setScrollEnabled:NO];
    NSLog(@"NO more scrolling!!!");
}

-(void) resumeScrolling
{    
    [scrollView setScrollEnabled:YES];
    NSLog(@"begin scrolling again!!");
}

如果有人有任何问题或无法实现这一点,我会很开心分享我的更多代码。我只是省略了大部分内容,因为它不是真正涉及到这个问题。

If anyone has questions or is having trouble implementing this, I'll be happy to share more of my code. I just omitted most of it because it doesn't really pertain to this subject.

这篇关于通过子视图中的IBAction禁用滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 14:41