2

嗨,我是 iphone 应用程序开发的新手。我想为我的 uiscroll 视图使用页面控制器。在单个视图中,我使用两个滚动视图和两个页面控件。我使用以下编码设置了 pagecontrol,但只有两个页面工作正常。但我想添加包含静态按钮的滚动视图超过两页。![在此处输入图像描述][1]

//
//  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 = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    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 {

    [self setupPage];



    //[scr_anger setScrollEnabled:YES];
    //[scr_anger setContentSize:CGSizeMake(1290,15)];

    //[scr_sketch setScrollEnabled:YES];
    //[scr_sketch setContentSize:CGSizeMake(1310,15)];

    [super viewDidLoad];
}

-(IBAction)buttonPressed:(id)sender {
        i_curtag=(int)[sender tag];
        //NSLog(@"%d",i_curtag);
        [self fn_btnOperation];
        //[self opt:str_filename];
        //NSLog(@"str_filename:%@",str_filename);
   }

-(void)fn_btnOperation
{
    if (i_curtag==1)
    {
        //str_filename=[NSString stringWithFormat:@"%d.png",i_curtag];
        //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");
    }

    [self opt:str_filename];
}

-(id)opt:(NSString*)filename
{
    //NSLog(@"filename:%@",filename);
    str_filename=filename;
    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@""delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Save Image" otherButtonTitles:@"Mail Image",nil];
    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [popupQuery showInView:self.view];
    [popupQuery release];   
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex1
{
    if (buttonIndex1 == 0)
    {
       // NSLog(@"str_filename in actionsheet:%@",str_filename);
        [self savesingleimage];
       // NSLog(@"save");
    } 

    else if (buttonIndex1 == 1)
    {
        //NSLog(@"str_filename in else:%@",str_filename);
        [self fmail];
        //NSLog(@"mail");
    }
}

-(void)fmail
{   
    //NSLog(@"str_filename:%@",str_filename);

        NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body><p>"] retain];

    [emailBody appendString:@"<b><u>EMOTIONAL ICONS</u></b><br><br>"];

    [emailBody appendString:@"</p></body></html>"];

        MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];

    UIImage *icon1 = [UIImage imageNamed:str_filename];
    NSData *imageData1 = UIImageJPEGRepresentation(icon1, 1);
    [emailDialog addAttachmentData:imageData1 mimeType:@"image/jpg" fileName:str_filename];
        emailDialog.mailComposeDelegate =self;
        [emailDialog setSubject:@"Emotional Icons"];
        [emailDialog setMessageBody:emailBody isHTML:YES];
        [emailDialog.navigationBar setTintColor:[UIColor blackColor]];

        [self presentModalViewController:emailDialog animated:YES];
        [emailDialog release];
        [emailBody release];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    UIAlertView *alert = [[UIAlertView alloc] 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;  
    }
    [self dismissModalViewControllerAnimated:YES];

    [alert show];
    [alert release];
}

-(void)savesingleimage{

   // NSLog(@"str_filename in save: %@",str_filename);
    UIImage *image1 = [UIImage imageNamed:str_filename];    
    UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil);
    UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@"Saved" 
        message:@"Image Saved into PhotoAlbum" 
        delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(110, 170, 50, 50)];
    NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:str_filename]];
    UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
    [imageView setImage:bkgImg];
    [bkgImg release];
    [path release];

    [successAlert addSubview:imageView];
    [imageView release];

    [successAlert show];
    [successAlert release];
}


#pragma mark -
#pragma mark The Guts
- (void)setupPage
{
    scr_anger.delegate = self;
    scr_sketch.delegate =self;

    //[self.scr_anger setBackgroundColor:[UIColor blackColor]];
    [scr_anger setCanCancelContentTouches:NO];
    [scr_sketch setCanCancelContentTouches:NO];

    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;


    [scr_anger setScrollEnabled:YES];
    [scr_sketch setScrollEnabled:YES];

    [scr_anger setContentSize:CGSizeMake(1290,15)];
    [scr_sketch setContentSize:CGSizeMake(1940,15)];

}

#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;

    [scr_anger scrollRectToVisible:frame animated:YES];
    /*
     *  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;

        [scr_sketch scrollRectToVisible:frame1 animated:YES];

        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.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [scr_anger release];
    [scr_sketch release];

    [pageControl release];
    [pageControl1 release];
        // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}

@end
4

3 回答 3

2

是一个关于 UIScrollView 和 IPageControl 组合的非常好的教程。但我自己寻找更多动态设计的教程(如 ios 应用程序的主页 - 我自动递增 - 递减)

于 2012-04-10T13:45:53.690 回答
0

这是一个信息:http ://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html

于 2012-04-10T13:28:56.477 回答
0

在您的setupPage:方法中,您有一个代码允许这样的页面数。

self.pageControl.numberOfPages = 2;
self.pageControl1.numberOfPages = 2;

如果要增加 numberOfPages,则增加这些行中的 numberOfPages。

于 2012-04-10T13:45:04.663 回答