ios开发6

来源:互联网 发布:淘宝数据魔方登陆 编辑:程序博客网 时间:2024/04/30 18:31
UIPageControl实现自定义按钮

有时候UIPageControl需要用到白色的背景那么会导致上面的点按钮看不见或不清楚,
我们可以通过继承该类重写函数来更换点按钮的图片现实.
实现思路如下.
新建类继承UIPageControl :
@interface MyPageControl : UIPageControl
{
    UIImage*imagePageStateNormal;
    UIImage*imagePageStateHighlighted;
}
- (id)initWithFrame:(CGRect)frame;
@property (nonatomic, retain) UIImage*imagePageStateNormal;
@property (nonatomic, retain) UIImage*imagePageStateHighlighted;
@end

声明了初始化该类的函数
用了两个UIImage保存两张图片大家知道的, UIPageCotrol的按钮分为两态一个是正常一个是高亮
接下来实现该类以及重写父类方法:
@interfaceMyPageControl(private)  // 声明一个私有方法该方法不允许对象直接使用
- (void)updateDots;
@end

@implementation MyPageControl  //实现部分

@synthesize imagePageStateNormal;
@synthesize imagePageStateHighlighted;

- (id)initWithFrame:(CGRect)frame { // 初始化
    self = [superinitWithFrame:frame];
    return self;
}

- (void)setImagePageStateNormal:(UIImage*)image {  // 设置正常状态点按钮的图片
    [imagePageStateNormalrelease];
    imagePageStateNormal= [image retain];
    [self updateDots];
}

-(void)setImagePageStateHighlighted:(UIImage *)image { // 设置高亮状态点按钮图片
    [imagePageStateHighlightedrelease];
    imagePageStateHighlighted= [image retain];
    [self updateDots];
}

- (void)endTrackingWithTouch:(UITouch*)touch withEvent:(UIEvent *)event { // 点击事件
    [superendTrackingWithTouch:touch withEvent:event];
    [self updateDots];
}

- (void)updateDots { // 更新显示所有的点按钮
    if(imagePageStateNormal || imagePageStateHighlighted)
    {
        NSArray*subview = self.subviews;  // 获取所有子视图
        for(NSInteger i = 0; i < [subview count]; i++)
        {
            UIImageView*dot = [subview objectAtIndex:i];  // 以下不解释看了基本明白
            dot.image= self.currentPage == i ? imagePageStateNormal : imagePageStateHighlighted;
        }
    }
}

- (void)dealloc { // 释放内存
    [imagePageStateNormalrelease], imagePageStateNormal = nil;
    [imagePageStateHighlightedrelease], imagePageStateHighlighted = nil;
    [super dealloc];
}

@end
OK, 在添加处加入以下来实例化该对象代码:
MyPageControl *pageControl =[[MyPageControl alloc] initWithFrame:CGRectMake(0,0, 200, 30)];
pageControl.backgroundColor = [UIColorclearColor];
pageControl.numberOfPages = 5;
pageControl.currentPage = 0;
[pageControlsetImagePageStateNormal:[UIImageimageNamed:@"pageControlStateNormal.png"]];
[pageControl setImagePageStateHighlighted:[UIImageimageNamed:@"pageControlStateHighlighted.png"]];
[self.view addSubview:pageControl];

[pageControl release];

iPhone电子书toolbar的实现
iPhone电子书的toolbar一般都设计成半透明,上面放置一个进度条和一个Label(用于显示页码),这里用代码做一个最基本的实现。
生成一个UIToolbar
UIToolbar *toolbar =[[[UIToolbar allocinitautorelease];
toolbar.barStyle=UIBarStyleBlackTranslucent;
[toolbar sizeToFit];
CGFloat toolbarHeight =[toolbar frame].size.height;
CGRect rootViewBounds =self.parentViewController.view.bounds;
CGFloat rootViewHeight =CGRectGetHeight(rootViewBounds);
CGFloat rootViewWidth =CGRectGetWidth(rootViewBounds);
CGRect rectArea = CGRectMake(0, rootViewHeight-toolbarHeight,rootViewWidth, toolbarHeight);
[toolbar setFrame:rectArea];
toolbar.backgroundColor= [UIColor clearColor];


生成一个Slider

UISlider*readSlider =[[[UISlideralloc]initWithFrame:CGRectMake(0,0, 225,30)] autorelease];
readSlider.minimumValue = 0.0f;
readSlider.maximumValue = 1.0f;
readSlider.continuous = YES;
readSlider.enabled = YES;


生成一个Label

UILabel*readLabel =[[[UILabelalloc]initWithFrame:CGRectMake(230,0, 50,30)] autorelease];
readLabel.backgroundColor = [UIColor clearColor];
readLabel.textColor =[UIColor whiteColor];


Slider和Label加入到toolbar中

NSMutableArray *tbitems =[NSMutableArray array];
[tbitems addObject:[[[UIBarButtonItem alloc]initWithCustomView:readSlider] autorelease]];
[tbitems addObject:[[[UIBarButtonItemalloc] initWithCustomView:readLabel]autorelease]]; 
toolbar.items = tbitems;


toolbar加入到当前view中 
[self.navigationController.view addSubview:toolbar];


点击屏幕即隐藏的功能,将toolbar的hidden属性置为YES即可

toolBar.hidden = YES;


UIView中bounds和frame的差别?
翻译文档上的
bounds是指这个view在它自己坐标系的坐标和大小 而frame指的是这个view在它superview的坐标系的坐标和大小
区别主要在坐标系这一块。

很明显一个是自己为原点的坐标系,一个是以屏幕为原点的坐标系。绝对坐标。。。相对坐标。。。比如屏幕旋转的时候就要以相对来重绘。 

frame 如果一个按钮,是在表格里,按钮的frame 的坐标也是相对的,并不是相对屏幕,也就是说是相对坐标,不是绝对坐标

我也想知道任何一个uiview如何求得它在屏幕上的坐标。

view 的frame是view在它的super view 的位置与尺寸。
view 的bounds可以用来帮助它的subview来定位的 ,layoutSubviews。


Frame  is  in  terms  of  superview's  coordinate  system   

框架是从父视图的坐标系统


Bounds   is  in  terms  of   local  coordinate  system
是在局部坐标系统

[ 此帖被haoxue在2011-11-26 16:07重新编辑 ]

图片:6_491_772cfea14e61028.png 
很明显,bounds的原点是(0,0)点,而frame的原点却是任意的。
frame 如果一个按钮,是在表格里,按钮的frame 的坐标也是相对的,并不是相对屏幕,也就是说是相对坐标,不是绝对坐标。
frame 是相对坐标。bounds是绝对坐标。
Android的开发过程中,绝对坐标,这样画出来的位置都是相对于屏幕的而不是相对于控件的

 什么是绝对坐标值,相对坐标值?
绝对坐标是:X,Y    就是相对于坐标原点的。                    
例如(15,20)相对坐标是:@X,Y   就是相对于参考点(可以是自己设定的一个点)。                  
   例如(15,20)相对于参考点(1,1)的坐标,表示:@14,19                            
(15,20)相对于参考点(-1,-1)的坐标,表示:@16,21

bounds是指这个view在它自己坐标系的坐标和大小 而frame指的是这个view在它superview的坐标系的坐标和大小.
区别主要在坐标系这一块。
很明显一个是自己为原点的坐标系,一个是以屏幕为原点的坐标系。

[ 此帖被haoxue在2011-11-26 16:12重新编辑 ]

图片:frame_bounds_rects.jpg 
多使用宏定义常量。tag,frame大小,一些判断标志位。
#define kIndexValueTag 1
苹果屏幕截图快捷键
一般在Mac上用Command-Shif-3/4来截图。注:Command=苹果键 其实还有几个辅助键,来起到不同的截图功能……
011)Command-Shift-3(适用于OS9,10.1X和10.2)02将整个屏幕拍下并保存到桌面。032)Command-Shift-4(适用于OS9,10.1X和10.2)04将屏幕的一部分拍下并保存到桌面。当按下着几个键后,光标会变为一个十字,可以拖拉来选取拍报区域。053)Command-Shift-Control-3(适用于OS9和10.2)
06将整个屏幕拍下并保存到剪贴板,可以Command+V直接粘贴到如Photoshop等软件中编辑。
074)Command-Shift-Control-4(适用于OS9和10.2)
08将屏幕的一部分拍下并保存到剪贴板。
095)Command-Shift-4再按空格键(适用于10.2)
10光标会变成一个照相机,点击可拍下当前窗口或菜单或Dock以及图标等,只要将照相机移动到不用区域(有效区域会显示为浅蓝色)点击。
116)Command-Shift-Control-4再按空格键(适用于10.2)
12将选取的窗口或其他区域的快照保存到剪贴板。
137)Command-Shift-Capslock-4(适用于OS9)
14将当前的窗口拍下并保存到桌面。
158)Command-Shift-Capslock-Control-4(适用于OS9)
16将当前的窗口拍下并保存到剪贴板。
设置透明度
 [myView setAlpha:value];   (0.0 < value < 1.0)设置背景色
1[myView setBackgroundColor:[UIColor redColor]];
(blackColor;darkGrayColor;lightGrayColor;whiteColor;grayColor; redColor; greenColor; blueColor; cyanColor;yellowColor;magentaColor;3orangeColor;purpleColor;brownColor; clearColor; )自定义颜色:
1UIColor *newColor = [[UIColor alloc] initWithRed:(float) green:(float) blue:(float) alpha:(float)];      0.0~1.0宽度和高度
1768X1024     1024X768    状态栏高 20 像素高   导航栏 工具栏 44像素高隐藏状态栏:
1[[UIApplication shareApplication] setStatusBarHidden: YES animated:NO]