我正在制作StoryBoard中的iPhone应用程序。它里面有一个UIScrollView
。在这里面,它有一个SliderView
,这是我写的一个自定义子类,它派生自UIView
。
在SliderView
中,我正在执行这个方法:
// Inside SliderView.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// whenever someone touches inside of SliderView, this method fires
}
我的问题是。在我的故事板中,我想禁用UIScrollView
的弹性垂直滚动。
正常情况下,这很容易。假设我已经连接了UIScrollView
作为IBOutlet
在应用程序的主视图控制器,我可以粘贴到该文件:
// Inside ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.bounces = NO;
}
不幸的是,这不是那么容易,因为我希望这种禁用只在touchesBegan
被触发时发生。
这是我完全被难住的地方:我怎么能得到SliderView。m文件与ViewController通信。m文件?
使用委托:
SliderView.h
// SliderView.h
@protocol SliderViewDelegate <NSObject>
@optional
- (void) isScrollViewToBounce:(BOOL)isBounce;
@end
@interface SliderView : UIView
@property (nonatomic, weak) id <SliderViewDelegate> sliderViewDelegate;
@end
SliderView.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.sliderViewDelegate isScrollViewToBounce:NO];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.sliderViewDelegate isScrollViewToBounce:YES];
}
ViewController.m
#import "SliderView.h"
@interface ViewController () <SliderViewDelegate>
@end
viewDidLoad()
method
- (void)viewDidLoad {
[super viewDidLoad];
self.sliderView setSlideViewDelegate:self];
}
在你的ViewController中创建Delegate方法:
- (void) isScrollViewToBounce:(BOOL)isBounce {
self.scrollView.bounces = isBounce;
}
也不要忘记删除委托
#pragma mark Memory management methods
//---------------------------------------------------------------
- (void)dealloc {
// Release resources here.
[self.scrollView setSliderViewDelegate:nil]
}