標籤:
今天玩了幾個控制項和手勢,讓我想起了以前玩Unity的時候,拖控制項,設定屬性,再編寫一些相應的值。
手勢
使用手勢的步驟:
1.建立滿足需求的手勢,在建立時關聯手勢觸發時的方法
2.配置手勢的相關屬性
3.將手勢添加到需要執行操作的視圖上面
4.實現手勢方法,當觸摸發生,手勢辨識器識別到相對應的觸摸時,就會執行關聯方法
輕拍手勢
1 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];2 //設定手勢3 //1.觸發手勢的手指個數4 tap.numberOfTouchesRequired = 1;5 //2.觸發手勢的輕拍次數6 tap.numberOfTapsRequired = 2;7 //3.讓imageView添加輕拍手勢8 [imageView addGestureRecognizer:tap];
輕拍手勢事件
1 - (void)tapAction:(UITapGestureRecognizer *)tap {2 }
撥動手勢
1 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@seletor(swipeAction:)];2 //手勢方向3 swipe.direction = UISwipeGestureRecognizerDirectionDown;4 //讓imageView添加輕拍手勢5 [imageView addGestureRecognizer:swipe];
撥動手勢事件
1 - (void)swipeAction:(UISwipeGestureRecognizer *)swipe {2 }
長按手勢
1 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];2 //長按手勢觸發的最短時間,預設是0.53 longPress.minimunPressDuration = 3.0;4 //讓imageView添加長按手勢5 [imageView addGestureRecognizer:longPress];
縮放手勢
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@seletor(pinAction:)];
縮放手勢事件
1 - (void)pinchAction:(UIPinchGestureRecognizer *)pinch { 2 //擷取要進行縮放的視圖 3 UIImageView *imageView = (UIImageView *)pinch.view; 4 5 //改變imageView的形變屬性 6 //第一個參數是需要已存在的transform,第二個參數是x方向的縮放規模,第三個參數是y方向的縮放規模 7 imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale); 8 pinch.scale = 1; 9 //或者10 // imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);11 }
旋轉手勢
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];
旋轉手勢事件
1 - (void)rotateAction:(UIRotationGestureRecognizer *)rotate { 2 //擷取想要旋轉的視圖 3 UIImageView *imageView = (UIImageView *)rotate.view; 4 5 //改變imageView的形變屬性 6 //回彈旋轉 7 // imageView.transform = CGAffineTransformMakeRotation(rotate.rotation); 8 imageView.transform = CGAffineTransformRotate(imageView.transform, rotate.rotation); 9 rotate.rotation = 0;10 }
平移手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
平移手勢事件
1 - (void)panAction:(UIPanGestureRecognizer *)pan {2 //擷取想要平移的視圖3 UIImageView *imageView = (UIImageView *)pan.view;4 5 CGPoint p = [pan translatinInView:imageView];6 //改變視圖的位置屬性7 imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);8 [pan setTranslation:CGPointZero inView:imageView];9 }
UISwitch
1.initWithFrame:。初始化方法,這個frame的寬和高沒有意義,因為系統開關控制項的大小是確定的。
2.onTintColor。設定開關開啟時的顏色。
3.tintColor。設定開關風格的顏色。
4.thumbTintColor。設定開關按鈕的顏色。
5.onImage/offImage。設定開關開啟/關閉狀態時的圖片(iOS 7以後不再起任何作用)。
6.on。開關狀態。
7.setOn: animated:。手動設定開關狀態,兩個參數均為BOOL類型。
UISlider
1.initWithFrame:。初始化方法。
2.minimumValue。設定滑塊的最小值。
3.mamximumValue。設定滑塊的最大值。
4.value。設定滑塊的當前值。
5.minimumTrackTintColor。定義划過地區的顏色。
UISegmentedControl
1.initWithItems:。初始化方法,參數可以為字串、圖片數組。
2.selectedSegmentIndex。指定被選中的分段,返回被選擇分段的索引值。
3.tintColor。設定segmentedControl條的顏色。
4.segmentedControlStyle。設定外觀樣式(已經不被推薦使用)。
5.momentary。設定在點擊後是否恢複原樣。
6.setTile: forSegmentAtIndex:。為指定下標的分段設定title,第一個參數為字串,第二個參數為下標值。
7.setImage: forSegmentAtIndex:。為指定下標的分段設定圖片,第一個參數為圖片,第二個參數為下標值。
8.setEnable: forSegmentAtIndex:。設定指定索引是否可點,第一個參數為BOOL值,第二個參數為下標值。
9.isEnableForSegmentAtIndex:。判斷指定索引是否可點,返回一個BOOL值。
UIPageControl
1.initWithFrame:。初始化方法。
2.numberOfPages。指定頁面的個數,即點的個數。
3.currentPage。指定pageControl的值,即選中的點。
【Objective-C學習記錄】第四十天