1. 程式人生 > >【XCode - OC】之UI基礎3

【XCode - OC】之UI基礎3

myboard中

在這裡插入圖片描述

純程式碼

- (void)viewDidLoad {
//1.1 建立按鈕物件,按鈕型別只能初始化時設定
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//1.2 設定位置和大小
button.frame = CGRectMake(100, 200, 200, 150);

//1.3 設定背景顏色
button.backgroundColor = [UIColor orangeColor];

//1.4 設定文字
[button setTitle:@"普通狀態" forState:UIControlStateNormal];
[button setTitle:@"高亮狀態" forState:UIControlStateHighlighted];

//1.5 設定文字顏色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

//1.6 設定文字陰影
[button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateHighlighted];
button.titleLabel.shadowOffset = CGSizeMake(3, 2);

//1.7 插入圖片
[button setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
//[button setBackgroundImage:[UIImage imageNamed:<#(nonnull NSString *)#>] forState:<#(UIControlState)#>];

//2.0 新增到View中
[self.view addSubview:button];

//3.0 設定監聽
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)click:(UIButton *)button{
button.enabled = NO;

}


UIScrollerView

 // 設定scrollerView的屬性
     scrollerView.contentSize = imageView.image.size;
     
     ------------
     UIScrollView *scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 100, 220, 500)];

[self.view addSubview:scrollerView];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
[scrollerView addSubview:imageView];
// 設定scrollerView的屬性
scrollerView.contentSize = imageView.image.size;
// 設定彈簧效果 預設有
//scrollerView.bounces = NO;
/*
UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
indicatorView.center = CGPointMake(100, -50);
[indicatorView startAnimating];
[scrollerView addSubview:indicatorView];
// 下拉重新整理用到
scrollerView.alwaysBounceVertical = YES;
scrollerView.alwaysBounceHorizontal = YES;
 */
// 預設位置偏移量
scrollerView.contentOffset = CGPointMake(100, 20);


移動到右下角:

- (IBAction)rightBottom {
[UIView animateWithDuration:2.0 animations:^{
    CGFloat offsetX = self.scrollerView.contentSize.width - self.scrollerView.frame.size.width;
    CGFloat offsetY = self.scrollerView.contentSize.height -self.scrollerView.frame.size.height;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    [self.scrollerView setContentOffset:offset];
}];

//[self.scrollerView setContentOffset:offset animated:YES];

}