6
我有一個水平 UIScrollview設置(意味着它不是一個上下滾動,但只有一個滾動左 - 右),並在應用程序啓動時,我想這個滾動視圖向左滾動,然後正確 - 種「展示」其滾動的能力 - 最後停止,讓用戶隨後用手指接管並手動控制滾動。 一切正常 - 除了這個有載左右演示滾動。如何以編程方式使用UIScrollView強制滾動?
我不使用Interface Builder任何的這個,我做的一切,代碼:
//(this is in viewDidLoad:)
// Create the scrollView:
UIScrollView *photosScroll = [[UIScrollView alloc] initWithFrame: CGRectMake(0, 0, 320, 200)];
[photosScroll setContentSize: CGSizeMake(1240, 200)];
// Add it as a subview to the mainView
[mainView addSubview:photosScroll];
// Set the photoScroll's delegate:
photosScroll.delegate = self;
// Create a frame to which to scroll:
CGRect frame = CGRectMake(10, 10, 80, 150);
// Scroll to that frame:
[photosScroll scrollRectToVisible: frame animated:YES];
所以了滾動荷載成功,我能夠把它的滾動左右與我的手指 - 但它不會像我希望的那樣「自動滾動」。
- 我試圖調用scrollRectToVisible前後添加 scrollview作爲子視圖 - 沒有工作。
- 思想也許這應該 中的loadView發生,而不是在viewDidLoad中?但如果是這樣,怎麼樣?
任何想法?
你的意思是這樣? CGRect frame = CGRectMake(800,0,800,150); [photosScroll scrollRectToVisible:frame animated:YES]; frame = CGRectMake(0,0,800,150); [photosScroll scrollRectToVisible:frame animated:YES];原因是沒有工作...... – sirab333
對於#1,是的,但你不需要讓'frame'滾動到整個視圖的大小。如果你喜歡,你可以做'CGRectMake(800,0,1,1)',然後'CGRectMake(0,0,1,1)'。從800個像素滾動區域1600個像素到視圖中,可滾動到'(1599,0,1,1)',然後從0至800的區域向後滾動到視圖,可以滾動到'(0 ,0,1,1)'。 – darvids0n
哦確定讓我在viewDidAppear試試這個... – sirab333