Chinaunix首页 | 论坛 | 博客
  • 博客访问: 397255
  • 博文数量: 466
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-16 13:59
文章分类

全部博文(466)

文章存档

2015年(466)

我的朋友

分类: C/C++

2015-03-16 14:25:55

原文地址:Ip 控件 代码片段 作者:linux_wuliqiang

001> main 人口函数代码: 

int main(int argc, char *argv[])
{

    // 创建一个全局的缓冲池
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate"); // 实例画一个主窗口

    [pool release];
    return retVal;
}


002> xxxAppDelegate 的主要初始化代码:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    HelloController *hello = [[HelloController alloc] init];
    [window addSubview:hello.view];
    [window makeKeyAndVisible];
}


// 导航面板框架

UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[HelloController alloc] init]];

[window addSubview:nav.view];

[window makeKeyAndVisible];



003> controller 中的方法, 当屏幕旋转的时候,会系统会调用此方法。


- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {

    CGRect apprect;
    apprect.origin = CGPointMake(0.0f, 0.0f);
    
    if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
        apprect.size = CGSizeMake(480.0f, 300.0f);
    else
        apprect.size = CGSizeMake(320.0f, 460.0f);

    // Iterate through the subviews and inset each item

    float offset = 32.0f;
    for (UIView *subview in [self.view subviews])
    {
        CGRect frame = CGRectInset(apprect, offset, offset);
        [subview setFrame:frame];
        offset += 32.0f;
    }
}


004> 代码片段:

[UIScreen mainScreen] bounds] ---得到屏幕的大小

[UIScreen mainScreen] applicationFrame]--得到屏幕的大小

CGRect apprect = [contentView bounds]; -- 得到 view 的大小

NSString *whichFlower = [[NSArray arrayWithObjects:@"blueFlower.png", @"pinkFlower.png", @"orangeFlower.png", nil] objectAtIndex:(random() % 3)];



005> xxxControll 内部方法

- (void)loadView
{
    // Create the main view -- 主 view
    UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
    contentView.backgroundColor = [UIColor whiteColor];
    self.view = contentView;
    [contentView release];

    // Get the view bounds as our starting point
    CGRect apprect = [contentView bounds];
    
    // Add each inset subview 
    UIView *subview = [[UIView alloc] initWithFrame:CGRectInset(apprect, 32.0f, 32.0f)];
    subview.backgroundColor = [UIColor lightGrayColor];
    [contentView addSubview:subview];
    [subview release];
    
    subview = [[UIView alloc] initWithFrame:CGRectInset(apprect, 64.0f, 64.0f)];
    subview.backgroundColor = [UIColor darkGrayColor];
    [contentView addSubview:subview];
    [subview release];
    
    subview = [[UIView alloc] initWithFrame:CGRectInset(apprect, 96.0f, 96.0f)];
    subview.backgroundColor = [UIColor blackColor];
    [contentView addSubview:subview];
    [subview release];
}


// demo 002

- (void)loadView

{

UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

contentView.image = [UIImage imageNamed:@"Default.png"];

contentView.userInteractionEnabled = YES;

self.view = contentView;

[contentView release];


self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]

  initWithTitle:@"Do It" 

  style:UIBarButtonItemStylePlain 

  target:self 

  action:@selector(doit)] autorelease];

}








006> 使用标志位 tag

UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 400.0f, 320.0f, 80.0f)];
colorView.tag = 100;  // 设置 tag 位
[self addSubview:colorView];


[self viewWithTag: 100].backgroundColor  // 从 tag 位得到 view 指针


007> 切换动画

    isVisible = !isVisible;    

    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];

    // 要执行的动作代码部分 ---设计到界面变换的代码都写到这个位置。
    [[self viewWithTag:IMAGE_VIEW_TAG] setAlpha:(float)isVisible];
    

    [UIView commitAnimations];  // 提交要执行的动画


//---------------------------------------------------------------

// Start Animation Block

CGContextRef context = UIGraphicsGetCurrentContext();

[UIView beginAnimations:nil context:context];

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationDuration:1.0];

// Animations  -- 切换动画

[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

// Commit Animation Block

[UIView commitAnimations]; // 提交要执行的动画

//---------------------
------------------------------------------


// 另一种节目切换动画,CATransition 类来实现

 

CATransition *animation = [CATransition animation];

[animation setDelegate:self];

[animation setDuration:1.0f];

[animation setTimingFunction:UIViewAnimationCurveEaseInOut];

[animation setType: kCATransitionPush];

[animation setSubtype: kCATransitionFromLeft];

[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];


        // 增加一个动画, 系统在界面切换的时候会自动调用此动画

[[[self superview] layer] addAnimation:animation forKey:@"transitionViewAnimation"];




008> 定时器 相关代码


// 启动一个定时器

[NSTimer scheduledTimerWithTimeInterval: 0.03f target: self selector: @selector(handleTimer:)                                 userInfo: nil repeats: YES];    

// 定时器回调函数
- (void) handleTimer: (NSTimer *) timer
{
      // doSomething
}


009> 按钮的初始化

// Add an action button
self.navigationItem.rightBarButtonItem = 

[[[UIBarButtonItem alloc]    // 内存分配

initWithTitle:@"Do It"       // 按钮显示文字                           style:UIBarButtonItemStylePlain // 按钮风格 

target:self                     // 按钮回调函数类实例                   action:@selector(actionFun)]   // 按钮回调函数方法

autorelease];


// 按钮按下的时候,动作响应函数
- (void) actionFun
{
}



010> AlertView ,弹出对话框相关



//-------------------------------------------------

//-- 弹出一个信息提示框,不进行任何回调处理

UIAlertView *baseAlert = [[UIAlertView alloc]
initWithTitle:@"Alert title"

message:@"test message" 

delegate:self 

cancelButtonTitle:nil

otherButtonTitles:@"OK", 

nil];

[baseAlert show];  // 显示到屏幕


//-------------------------------------------------

//-- 有回调函数的信息框

UIAlertView *baseAlert = [[UIAlertView alloc]

initWithTitle:@"Alert title" 

message:@"Please select a button" 

delegate:self     // 回调事件处理类实例

cancelButtonTitle:nil

// 按钮列表,nil 作为结束标志

otherButtonTitles:@"One", @"Two", @"Three",@"four", @"five", nil];


[baseAlert show];


// 信息框回调函数, UIAlertViewDelegate 协议的一个方法, 此类必须实现 UIAlertViewDelegate  协议

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

printf("User Pressed Button %d\n", buttonIndex + 1);

[alertView release];

}

//-------------------------------------------------

//-- time alart 

baseAlert = [[UIAlertView alloc

  initWithTitle:@"Alert" message:@"\waiting 3s." 

  delegate:self cancelButtonTitle:nil

  otherButtonTitles: nil];

[baseAlert show];


//--- 通过定时器来取消信息框的显示

[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector: @selector(performDismiss:)

  userInfo:nil repeats:NO];


阅读(295) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~