inside doesn create xcode uiview delegates uiscrollview ibaction

xcode - doesn - uiscrollview programmatically swift



Deshabilitar desplazamiento por IBAcción contenida en subvista (1)

Resuelto Incorporé Notificaciones en lugar de intentar llamar directamente un método desde una subvista.

Descripción rápida de mi configuración. UIViewController FishIDView contiene un UIScrollView llamado scrollView. scrollView tiene una subvista XMLView. XMLView tiene un botón que muestra su subvista, flipView. Quería que se deshabilitara el desplazamiento en scrollView cuando flipView estaba visible.

En XMLView.h (con el código irrelevante omitido):

#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

En 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

Asegúrese de poner #import "XMLView.h" en su archivo FishIDView.h en la parte superior y cree los métodos - (nulo) en la parte inferior.

#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

Luego en 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!!"); }

Si alguien tiene preguntas o tiene problemas para implementar esto, estaré encantado de compartir más de mi código. Simplemente omití la mayoría porque no pertenece realmente a este tema.

Mi aplicación para iPad está construida actualmente como tal: los datos xml se extraen de mi sitio web y se analizan en un controlador de vista "FishIDView". Este controlador de vista tiene un UIScrollView "scrollView" que tiene una subvista "XMLView" que es un UIView. scrollView está configurado para que se desplace y páginas a través de XMLViews en función del número de elementos contenidos en el XML. Por ejemplo, 12 elementos = 12 páginas o instancias de XMLView. Estas páginas contienen fotos de peces individuales. Cada una de estas páginas contiene un botón que crea una subvista de UIView "XMLView" llamada "flipView", y muestra la subvista mediante el salto de la transición animada izquierda. Esta subvista contiene información sobre los peces representados. flipView tiene un botón que devuelve al usuario (voltea hacia atrás) a la imagen de los peces.

Mi problema es que cuando miro el flipView (que es una subvista de XMLView que es una subvista de scrollView), el desplazamiento aún está habilitado. Si me desplazo hacia la izquierda o hacia la derecha mientras estoy en flipView, veo el XMLView del próximo pez. Quiero que se deshabilite el desplazamiento mientras miro en flipView.

¿Alguna sugerencia sobre lo que puedo hacer para enviar algo como un setScrollEnabled: NO comando para scrollView (en FishIDView) desde la subvista?

EDITAR:

Entonces asumo que necesito usar un protocolo y delegar. Pensé que podría resolverlo, pero me estoy poniendo al día con la implementación.

En mi XMLView.h (dejando fuera el código extraño):

@protocol XMLViewDelegate <NSObject> - (void) stopScrolling; @end @interface XMLView : UIView { ... id secondDelegate; } ... @property (nonatomic, retain) id <XMLViewDelegate> secondDelegate; ... @end

Luego en mi XMLView.m (con el IBAction enganchado a un UIButton que funciona correctamente):

... @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"); }

En FishIDView.h:

@interface FishIDView : UIViewController <XMLViewDelegate>

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!!!"); }

Cuando hago clic en mi botón en XMLView que activa "goToInfo", el resto de la acción ocurre, pero el registro "¡No más desplazamientos!" nunca se muestra

¿Lo hice de la manera incorrecta? Todavía estoy intentando descubrir delegados, así que puedo estar completamente equivocado con esta metodología.

EDICION 2:

Ahora he tratado de deshacerme del delegado y seguir lo que parece una ruta más simplista, pero todavía no está funcionando correctamente.

En 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"); }

Luego en FishIDView.h

-(void) stopScrolling;

FishIDView.m

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

El "No más desplazamientos !!!" imprime, ¡pero scrollView aún está habilitado con scroll! ¿Por qué debería ejecutar la porción NSLog de stopScrolling pero no establecerScrollEnabled: NO? ¿Alguien puede ayudarme?