• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何为uiscrollview添加页面控件?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:33:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

嗨,我是 iphone 应用程序开发的新手。我想为我的 uiscroll View 使用页面 Controller 。在单个 View 中,我使用两个 ScrollView 和两个页面控件。我使用以下编码设置了 pagecontrol,但只有两个页面工作正常。但是我想为包含静态按钮的 ScrollView 添加两个以上的页面。![在此处输入图像描述][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)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)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)buttonPressedid)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)optNSString*)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)actionSheetUIActionSheet *)actionSheet clickedButtonAtIndexNSInteger)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)mailComposeControllerMFMailComposeViewController*)controller didFinishWithResultMFMailComposeResult)result errorNSError*)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)scrollViewDidScrollUIScrollView *)_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



Best Answer-推荐答案


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

关于ios - 如何为uiscrollview添加页面控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10089593/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap