gpt4 book ai didi

objective-c - 以编程方式在 UIView 中添加 UIScrollView

转载 作者:太空狗 更新时间:2023-10-30 03:32:27 24 4
gpt4 key购买 nike

请教如何在UIView中添加UIScrollView,使UIScrollView正常工作。

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, container.frame.size.width/2, container.frame.size.height/2)];
scroll.pagingEnabled = YES;
scroll.scrollEnabled = YES;
[scroll setBackgroundColor:[UIColor redColor]];
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * container.frame.size.width/2;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, container.frame.size.width, container.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(container.frame.size.width/2 * numberOfViews, container.frame.size.height);
[container addSubview:scroll];

以上代码来自教程:http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/但这对我不起作用。

编辑:

如果您在正确设置 ScrollView 时遇到问题但它不工作,请确保您没有与另一个 UIView 覆盖您的 ScrollView 。那是我的问题。已解决!

最佳答案

您可以使用以下代码在您的 View 上添加 UIScrollView :-

第一步:

Delegate "UIScrollViewDelegate" to your ViewController.h

for example:
@interface yourViewController:UIViewController<UIScrollViewDelegate>

第 2 步:

//create you UIScrollView
UIScrollView *MyScrollVw= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,320 ,480)];
MyScrollVw.delegate= self;
[MyScrollVw setShowsHorizontalScrollIndicator:NO];
[MyScrollVw setShowsVerticalScrollIndicator:YES];
MyScrollVw.scrollEnabled= YES;
MyScrollVw.userInteractionEnabled= YES;
[yourView addSubview:MyScrollVw];
MyScrollVw.contentSize= CGSizeMake(320 ,1500);//(width,height)

第 3 步:

你想在 ViewController.m 中实现 scrollView Delegates

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return imgView;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"Did end decelerating");
//do your code here
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"Did scroll");
//do your code here
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"Did end dragging");
//do your code here
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"Did begin decelerating");
//do your code here
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"Did begin dragging");
//do your code here
}

关于objective-c - 以编程方式在 UIView 中添加 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16035115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com