IOS学习笔记

一、切换页面时的动画

[UIView beginAnimations:@"View Flip" context:nil]; //设置动画块
[UIView setAnimationDuration:1.25];  //动画时间
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  // 动画曲线
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; // 动画转换类型
[UIView commitAnimations]; //提交动画

二、页面左右滑动切换

用到比较多的还是UIScrollerView,再加一个UIPageControl,然后再加往UIScrollView中添加子View,并设置子View的frame.origin.x的值,最后调用UIScrollerView的scrollRectToViewsible,实现滚动。代码如下:

PageController.h
//
//  PageController.h
//  controller
//
//  Created by cdy on 12-7-29.
//  Copyright (c) 2012年 cdy. All rights reserved.
//

#import "ViewController.h"

@interface PageController : ViewController<UIScrollViewDelegate> {
    UIScrollView *contentView;
    UIPageControl *pageControl;
}
@property (nonatomic, retain) IBOutlet UIScrollView *contentView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage:(id)sender;
@end

PageController.m
//
//  PageController.m
//  controller
//
//  Created by cdy on 12-7-29.
//  Copyright (c) 2012年 cdy. All rights reserved.
//

#import "PageController.h"

@interface PageController ()

@end

@implementation PageController
@synthesize contentView,pageControl;

static NSUInteger kNumberOfPages = 5;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    contentView.pagingEnabled = YES;
    contentView.contentSize = CGSizeMake(contentView.frame.size.width * kNumberOfPages, contentView.frame.size.height);
    contentView.showsHorizontalScrollIndicator = NO;
    contentView.showsVerticalScrollIndicator = NO;
    contentView.scrollsToTop = NO;
    contentView.delegate = self;
    pageControl.numberOfPages = kNumberOfPages;
    pageControl.currentPage = 0;
    [self loadViewsIntoScrollerViews];
}

-(void)loadViewsIntoScrollerViews {
    for (int i=0; i<kNumberOfPages; i++) {
        NSString* identify = [[NSString alloc]initWithFormat:@"Controller%d",i];
        UIViewController* controller = [self getUIViewControllerFromStoryboard:identify];
        CGRect frame = contentView.frame;
        frame.origin.x = frame.size.width * i;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [contentView addSubview:controller.view];
    }
}
// 从Storyboard中获取UIViewController
-(UIViewController*)getUIViewControllerFromStoryboard:(NSString*)identify {
    return (UIViewController*)[self.storyboard instantiateViewControllerWithIdentifier:identify];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)changePage:(id)sender {
    int page = pageControl.currentPage;
    CGRect frame = contentView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [contentView scrollRectToVisible:frame animated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

三、用Xcode4.5部署在低版本上运行的时找不到Autolayout类的解决方法

用InterfaceBuilder打开xib文件,单击View,在右边窗口的第一项中,将“Use Autolayout”的勾去了,就正常了。


四、获取手指在控件的触摸位置

覆盖touchesBegan方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    touchDownTime = CFAbsoluteTimeGetCurrent();
    NSSet *allTouches = [event allTouches];  
    UITouch *touch = [allTouches anyObject];  
    CGPoint point=[touch locationInView:[touch view]];  
    int x=point.x;
    int y=point.y;
    NSLog(@"touchesBegan touch (x,y) is (%i,%i)",x,y);
}

五、利用图像的自动伸缩功能,解决自定义UISlider时设置了setMinimumTrackImage, 拖动时产生的图像变形的问题

用UIImage的 (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight得到一个自动伸缩的图片即中间伸缩,四角不变。

offImage_ = [[UIImage imageNamed:@"滑动条_红.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
        onImage_ = [UIImage imageNamed:@"滑动条_白.png"];
        thumbImage_ = [UIImage imageNamed:@"滑轮控制.png"];
        [self setThumbImage:thumbImage_ forState:UIControlStateNormal];
        [self setThumbImage:thumbImage_ forState:UIControlStateHighlighted];
        [self setMinimumTrackImage:offImage_ forState:UIControlStateNormal];
        [self setMaximumTrackImage:onImage_ forState:UIControlStateNormal];

六、IOS的按HOME键后的600s后台执行功能
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    UIApplication  *app = [UIApplication sharedApplication];
    __block UIBackgroundTaskIdentifier background_task;
    //后台10分钟会被回调
    background_task = [app beginBackgroundTaskWithExpirationHandler:^
                       {
                           NSLog(@"end Background!");
                           [app endBackgroundTask: background_task];
                           background_task = UIBackgroundTaskInvalid;
                           //关闭socket
                           [[SessionManger shareSessionManger] closeSession];
                       }];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"start Backgound!");
        //开启socket
        [[SessionManger shareSessionManger] startSession];
    });
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值