ios - 如何为uiscrollview添加页面控件?
<p><p>嗨,我是 iphone 应用程序开发的新手。我想为我的 uiscrollView 使用页面 Controller 。在单个 View 中,我使用两个 ScrollView 和两个页面控件。我使用以下编码设置了 pagecontrol,但只有两个页面工作正常。但是我想为包含静态按钮的 ScrollView 添加两个以上的页面。![在此处输入图像描述]</p>
<pre><code>//
//newsampleViewController.m
//newsample
//
//Created by SmartJobDevelopers on 4/3/12.
//Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "newsampleViewController.h"
@implementation newsampleViewController
@synthesize scr_anger;
@synthesize scr_sketch;
@synthesize pageControl;
@synthesize pageControl1;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = ;
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
;
//;
//;
//;
//;
;
}
-(IBAction)buttonPressed:(id)sender {
i_curtag=(int);
//NSLog(@"%d",i_curtag);
;
//;
//NSLog(@"str_filename:%@",str_filename);
}
-(void)fn_btnOperation
{
if (i_curtag==1)
{
//str_filename=;
//NSLog(@"str_filename:%@",str_filename);
str_filename=@"1.png";
// NSLog(@"1.png");
}
else if(i_curtag==2)
{
str_filename=@"2.png";
//NSLog(@"2.png");
}
else if(i_curtag==3)
{
str_filename=@"3.png";
//NSLog(@"3.png");
}
else if(i_curtag==4)
{
str_filename=@"4.png";
// NSLog(@"4.png");
}
else if(i_curtag==5)
{
str_filename=@"5.png";
//NSLog(@"5.png");
}
else if(i_curtag==6)
{
str_filename=@"6.png";
//NSLog(@"6.png");
}
else if(i_curtag==7)
{
str_filename=@"7.png";
// NSLog(@"7.png");
}
else if(i_curtag==8)
{
str_filename=@"8.png";
//NSLog(@"8.png");
}
else if(i_curtag==9)
{
str_filename=@"9.png";
// NSLog(@"9.png");
}
else if(i_curtag==10)
{
str_filename=@"10.png";
// NSLog(@"10.png");
}
;
}
-(id)opt:(NSString*)filename
{
//NSLog(@"filename:%@",filename);
str_filename=filename;
UIActionSheet *popupQuery = [ initWithTitle:@""delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Save Image" otherButtonTitles:@"Mail Image",nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
;
;
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex1
{
if (buttonIndex1 == 0)
{
// NSLog(@"str_filename in actionsheet:%@",str_filename);
;
// NSLog(@"save");
}
else if (buttonIndex1 == 1)
{
//NSLog(@"str_filename in else:%@",str_filename);
;
//NSLog(@"mail");
}
}
-(void)fmail
{
//NSLog(@"str_filename:%@",str_filename);
NSMutableString *emailBody = [[ initWithString:@"<html><body><p>"] retain];
;
;
MFMailComposeViewController *emailDialog = [ init];
UIImage *icon1 = ;
NSData *imageData1 = UIImageJPEGRepresentation(icon1, 1);
;
emailDialog.mailComposeDelegate =self;
;
;
];
;
;
;
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
UIAlertView *alert = [ initWithTitle:@"Status:" message:@"" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
switch (result) {
case MFMailComposeResultCancelled:
alert.message = @"Message Canceled";
break;
case MFMailComposeResultSaved:
alert.message = @"Message Saved";
break;
case MFMailComposeResultSent:
alert.message = @"Message Sent";
break;
case MFMailComposeResultFailed:
alert.message = @"Message Failed";
break;
default:
alert.message = @"Message Not Sent";
break;
}
;
;
;
}
-(void)savesingleimage{
// NSLog(@"str_filename in save: %@",str_filename);
UIImage *image1 = ;
UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil);
UIAlertView *successAlert = [ initWithTitle:@"Saved"
message:@"Image Saved into PhotoAlbum"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
UIImageView *imageView = [ initWithFrame:CGRectMake(110, 170, 50, 50)];
NSString *path = [ initWithString:[[ resourcePath] stringByAppendingPathComponent:str_filename]];
UIImage *bkgImg = [ initWithContentsOfFile:path];
;
;
;
;
;
;
;
}
#pragma mark -
#pragma mark The Guts
- (void)setupPage
{
scr_anger.delegate = self;
scr_sketch.delegate =self;
//];
;
;
scr_anger.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scr_anger.clipsToBounds = YES;
scr_anger.scrollEnabled = YES;
scr_anger.pagingEnabled = YES;
scr_sketch.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scr_sketch.clipsToBounds = YES;
scr_sketch.scrollEnabled = YES;
scr_sketch.pagingEnabled = YES;
self.pageControl.numberOfPages = 2;
self.pageControl1.numberOfPages = 2;
;
;
;
;
}
#pragma mark -
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
// NSLog(@"scrollview val=%d",_scrollView.tag);
i=_scrollView.tag;
// NSLog(@"i=%d",i);
if (_scrollView.tag==101) {
if (pageControlIsChangingPage) {
return;
}
/*
*We switch page at 50% across
*/
//scr_anger.frame = CGRectMake(0, 0, 320, 0);
CGFloat pageWidth =scr_anger.frame.size.width;
//NSLog(@"pagewidth=%f",pageWidth);
int page = floor((scr_anger.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
// NSLog(@"page=%d",page);
pageControl.currentPage = page;
}
else if(_scrollView.tag==102)
{
if (pageControlIsChangingPage) {
return;
}
/*
*We switch page at 50% across
*/
CGFloat pageWidth =scr_sketch.frame.size.width;
int page = floor((scr_sketch.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl1.currentPage = page;
NSLog(@"page=%d",page);
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
pageControlIsChangingPage = NO;
}
#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender
{
/*
*Change the scroll view
*/
//NSLog(@"i=%d",i);
if (i==101) {
CGRect frame = scr_anger.frame;
//pageControl.frame = CGRectMake(0, 390, 320, 15);
frame.origin.x = frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
;
/*
*When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
*/
pageControlIsChangingPage = YES;
}
else if(i==102)
{
CGRect frame1 = scr_sketch.frame;
frame1.origin.x = frame1.size.width * pageControl1.currentPage;
frame1.origin.y = 0;
;
pageControlIsChangingPage = YES;
}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
;
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
;
;
;
;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
;
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p> <a href="http://www.iosdevnotes.com/2011/03/uiscrollview-paging/#comment-14154" rel="noreferrer noopener nofollow">This</a>是一个关于 UIScrollView 和 IPageControl 组合的非常好的教程。但我自己寻找更多动态设计的教程(如 ios 应用程序的主页 - 我自动递增 - 递减)</p></p>
<p style="font-size: 20px;">关于ios - 如何为uiscrollview添加页面控件?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/10089593/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/10089593/
</a>
</p>
页:
[1]