问题:
我遇到的问题是当我放大我的UIScrollView(启用分页),然后我平移(滚动?) - 我可以拖过我的视图并看到大面积的scrollview的背景。我进一步放大了,UIScrollView的背景就越多了。 (我已将scrollview的背景设置为绿色,因此我可以告诉我正在查看的内容。)
不确定为什么背景区域会像那样曝光......?
我已经玩过在各个位置设置scrollview的帧大小,边界大小,内容大小等无济于事。
不幸的是,由于成为新用户,我无法发布最新消息的截图:(
更多详情:
我的应用程序的主视图包含一个全屏大小的UIScrollView,其中填充了许多UIView子视图。通过在for循环中创建nib名称,然后使用loadNibNamed方法检索它们来创建&amp ;,将这些子视图添加到scrollview中。添加UIView。
这些UIViews中的每一个都是在IB中设计的,包含以下内容:一个UIImageView,其图像属性设置为.jpg图像,另一个较小的UIScrollView,以及透明的UIView在我的imageview顶部,其中包含一堆自定义UIButtons。
在我填充scrollView之后,我设置了一些属性,例如contentSize,pagingEnabled,min& maxZoomScale等&然后将它的委托设置为self(MainViewController)。
MainViewController的相关方法:
@implementation MainViewController_iPhone
@synthesize scrollView, loadedNibView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self loadNibs];
[self initializeOrientation];
}
-(void) loadNibs {
currentPage = 0;
for (int i = 0; i < NUM_VIEWS; i++) {
loadedNibView = [self getNibView:i]; //load the UIView from the associated nib file
loadedNibView.tag = i; //used later to retrieve view for current page
[scrollView addSubview:loadedNibView];
}
//Set the content size to sum of the widths of the all UIViews
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * NUM_VIEWS, self.view.frame.size.height);
scrollView.pagingEnabled = YES;
scrollView.bounces = YES;
scrollView.clipsToBounds = YES;
//scrollView.alwaysBounceVertical = YES;
//zoom features
scrollView.maximumZoomScale = 5.0;
scrollView.minimumZoomScale = 1;
scrollView.delegate = self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)myScrollView {
return [myScrollView viewWithTag:(currentPage)];
}
- (void)scrollViewDidScroll:(UIScrollView *)myScrollView {
CGFloat pageWidth = myScrollView.frame.size.width;
currentPage = floor((myScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}
- (void)scrollViewDidZoom:(UIScrollView *)myScrollView {
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)myScrollView withView:(UIView *)view
{
//zooming & paging do not work well together; disable paging while we're zooming
myScrollView.pagingEnabled = NO;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)myScrollView withView:(UIView *)view atScale:(float)scale
{
if(scale == 1) //if we're zoomed entirely back out
{
//re-enable paging & reset the content size
myScrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * NUM_VIEWS, self.view.frame.size.height); //without this line, can no longer page through scrollview after zooming
}
}
@end
这是.h:
@interface MainViewController_iPhone : MainViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollView; //The main ScrollView that scrolls between pages of different images to search
UIView *loadedNibView; //The UIView that we load from it's corresponding nib (i.e. IMG1_iPhone.xib)
int currentPage;
}
//methods
-(void) loadNibs;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)myScrollView;
- (void)scrollViewWillBeginZooming:(UIScrollView *)myScrollView withView:(UIView *)view;
- (void)scrollViewDidEndZooming:(UIScrollView *)myScrollView withView:(UIView *)view atScale:(float)scale;
- (void)scrollViewDidZoom:(UIScrollView *)myScrollView;
- (void)scrollViewDidScroll:(UIScrollView *)myScrollView;
//properties
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIView *loadedNibView;
@end
非常感谢任何想法/建议......谢谢!
答案 0 :(得分:0)
好的,我弄清楚我的问题是什么。
我意识到,当我放大第一张图片时,它给了我奇怪的行为,滚动视图背景随着我缩放而增长,但如果我放大第二张图像 - 没有奇怪的行为。所以我开始想知道这两个图像之间有什么区别......并且意识到它必须是标签。在for循环中我加载视图&amp;设置他们的标签我将第一个设置为零,并从那里递增。哎呀 - 有点忘了还有很多其他观点&amp;默认标记为零的对象!所以后来当它要求带有标签0的视图时 - 它返回了一些其他视图&amp;这是一个调整大小而不是我的形象。
修正:
#define ZOOM_TAG 1000
...
for (int i = 0; i < NUM_VIEWS; i++) {
loadedNibView = [self getNibView:i];
//loadedNibView.tag = i
loadedNibView.tag = i + ZOOM_TAG; //used later to retrieve view for current page
[scrollView addSubview:loadedNibView];
}
...
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)myScrollView {
//return [myScrollView viewWithTag:(currentPage)];
return [myScrollView viewWithTag:(currentPage + ZOOM_TAG)];
}