ios - 使用跟随手指ios显示两个隐藏 View
<p><p>您好,我正在为 iPhone 开发一个应用程序,但无法显示 2 个隐藏 View 。
我在这里发布我的 Storyboard:<a href="http://postimg.org/image/v6nhepqqd/" rel="noreferrer noopener nofollow">http://postimg.org/image/v6nhepqqd/</a>
我想显示 View- 当我从左向右滑动手指时显示全部显示,然后我想显示 View- 关于当我从右向左滑动手指时。滑动手势必须类似于 youtube 菜单。
我只使用了一个 ViewController,我将在这里发布我的代码:</p>
<p><strong>ViewController.h</strong></p>
<pre><code>#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *viewAllShow;
@property (weak, nonatomic) IBOutlet UIView *viewMain;
@property (weak, nonatomic) IBOutlet UIView *viewAbout;
- (IBAction)buttonAllShow:(id)sender;
- (IBAction)buttonInfo:(id)sender;
@end
</code></pre>
<p><strong>ViewController.m</strong></p>
<pre><code>#import "ViewController.h"
@interface ViewController ()
@property BOOL viewMainShowed;
@end
@implementation ViewController
- (void)viewDidLoad
{
;
// self.viewScroll.contentSize = self.viewAbout.frame.size;
self.viewMainShowed = YES;
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated {
}
#pragma mark - actions -
- (IBAction)buttonAllShow:(id)sender {
// Controllo se la vista nascosta non è già visibile
if (self.viewMain.frame.origin.x == 0) {
// Chiamata alla funzione per la visualizzazione della vista nascosta
;
} else {
// Chiamata alla funzione per nascondere la vista nascosta
;
}
}
- (IBAction)buttonInfo:(id)sender {
if (self.viewMain.frame.origin.x == 0) {
;
} else {
;
}
}
#pragma mark - animations -
- (void)showAllShowView {
// Faccio partire l'animazione
[UIView animateWithDuration:0.5
animations:^{
;
}
];
self.viewMainShowed = NO;
}
- (void)hideAllShowView {
[UIView animateWithDuration:0.5
animations:^ {
;
}
];
self.viewMainShowed = YES;
}
- (void)showAboutView {
[UIView animateWithDuration:0.5
animations:^{
;
}
];
[UIView animateWithDuration:0.5
animations:^{
;
}];
self.viewMainShowed = NO;
}
- (void)hideAboutView {
[UIView animateWithDuration:0.5
animations:^{
;
}
];
[UIView animateWithDuration:0.5
animations:^{
;
}];
self.viewMainShowed = YES;
}
#pragma mark - touch event -
float difference;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint mainTouchPoint = [ locationInView:self.viewMain];
difference = mainTouchPoint.x;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pointInView = [ locationInView:self.view];
float xTarget = pointInView.x - difference;
if (xTarget > self.viewAllShow.frame.size.width) {
xTarget = self.viewAllShow.frame.size.width;
} else if (xTarget < 0) {
if (!self.viewMainShowed) {
xTarget = 0;
[UIView animateWithDuration:0.5
animations:^{
;
}
];
[UIView animateWithDuration:0.5
animations:^{
;
}
];
self.viewMainShowed = YES;
}
else {
xTarget = self.viewAbout.frame.size.width * -1;
[UIView animateWithDuration:0.5
animations:^{
;
}
];
[UIView animateWithDuration:0.5
animations:^{
;
}
];
self.viewMainShowed = NO;
}
} else if (xTarget > 0 && xTarget < self.viewAllShow.frame.size.width) {
xTarget = self.viewAllShow.frame.size.width;
self.viewMainShowed = NO;
[UIView animateWithDuration:0.5
animations:^{
;
}
];
}
// [UIView animateWithDuration:0.5
// animations:^{
// ;
// }
// ];
//self.viewMainShowed = NO;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint endPoint = [locationInView:self.view];
float xTarget = endPoint.x - difference;
if (xTarget < self.viewAllShow.frame.size.width/2) {
if (!self.viewMainShowed) {
xTarget = 0;
[UIView animateWithDuration:0.5
animations:^{
;
}
];
} else {
xTarget = self.viewAbout.frame.size.width * -1;
[UIView animateWithDuration:0.5
animations:^{
;
}
];
self.viewMainShowed = NO;
}
} //else
if (xTarget > 0 && xTarget < self.viewAllShow.frame.size.width){
xTarget = self.viewAllShow.frame.size.width;
[UIView animateWithDuration:0.5
animations:^{
;
}
];
self.viewMainShowed = NO;
}
if (xTarget < 0) {
xTarget = 0;
[UIView animateWithDuration:0.5
animations:^{
;
}
];
self.viewMainShowed = YES;
}
// [UIView animateWithDuration:0.5
// animations:^{
// ;
// }
// ];
//self.viewMainShowed = NO;
}
@end
</code></pre>
<p>我使用了一个 BOOL 变量来检查主视图是否处于事件状态。我在这个网站上创建了移动和检测触摸的代码:<a href="http://divcode.blogspot.it/2012/09/hidden-menu-part-2-following-finger.html" rel="noreferrer noopener nofollow">http://divcode.blogspot.it/2012/09/hidden-menu-part-2-following-finger.html</a>
其他人说我使用 UIPanGestureRecognizer,但是如果我想使用我在这里发布的方法,我应该怎么做?我的问题是,当我尝试在我的模拟器/真实 iPhone 上做一些手势时,它会向我显示错误的 View ,或者它会移动应该为我所做的手势修复的 View 。
我该如何解决这个问题?你能帮我剪下代码吗?
谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我认为您应该使用另一种方法来解决此问题:使用 UIPanGestureRecognizer 并通过速度检测手势的方向。
再见</p></p>
<p style="font-size: 20px;">关于ios - 使用跟随手指ios显示两个隐藏 View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/17069367/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/17069367/
</a>
</p>
页:
[1]